Ignoring special characters

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Ignoring special characters

This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 years, 9 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #28080
    msamavi64
    msamavi64
    Participant

    Hi Ernest,

    Please let me describe a bit about my requirements as we talked earlier:

    In my Woocommerce website I have products that have special characters in their titles, for example:

    Dīvān-i ashʻār-i Jalāl Ṭabīb Shīrāzī / Naṣr Allāh Pūrjavādī

    I would like the visitors to be able to find the above product using any of the following search phrases:

    divani
    ashari
    tabib
    nasr
    purjav

    The html character codes I sent to you in my previous message were related to the special characters you see on the top and bottom of some the normal characters of the above-mentioned title example (īʻāṬṣāūāī)

    If you guarantee that you will solve this for me, I will buy your plugin immediately.

    Kind regards,
    Majid

    #28087
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Majid,

    So, my suggestion is using a custom code with the index table engine. The custom code will try to replace these characters with the single unicode versions on both the server and client side.

    Please follow the instructions below:

    1. Please add this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter('asp_indexing_string_pre_process', 'custom_chars_asp_indexing_string_pre_process', 10, 1);
    add_filter('asp_search_phrase_before_cleaning', 'custom_chars_asp_indexing_string_pre_process', 10, 1);
    add_filter('asp_query_args', 'custom_chars_asp_indexing_string_pre_process', 10, 1);
    function custom_chars_asp_indexing_string_pre_process($s) {
    	$original = array(
    		'ī', 'ʻ', 'ā', 'Ṭ', 'ṣ', 'ū'
    	);
    	$replace = array(
    		'i', '', 'a', 't', 's', 'u'
    	);
    	
    	// Replace them
    	if ( is_array($s) ) {
    		if ( isset($s['s']) && !$s['_ajax_search'] ) 
    			$s['s'] = str_replace($original, $replace, $s['s']);      
    	} else {
    		$s = str_replace($original, $replace, $s);
    	}
    	
    	return $s;
    }

    2. Make sure to configure the index table engine. Follow the tutorial.
    3. Once ready and the indexing is finished. Make sure the index table is enabled

    If all goes right, entering the words should yield results. The code will only replace the given characters, only the lowercase versions. For this to work with the upper case, it needs to be added as well.

    • This reply was modified 3 years, 9 months ago by Ernest Marcinko Ernest Marcinko. Reason: Fixed code
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28089
    msamavi64
    msamavi64
    Participant

    Hi Ernest,

    Thank you for the reply.

    The search engine was already set to index table.
    I created a snippet containing your code.
    I deleted the index.
    I clicked on “Create new index” button, but it indexed only one item. (problem!)
    I disabled your code snippet.
    I clicked on “Create new index” button, then all items were indexed successfully as before.

    It seems the code snippet prevents indexing.
    Any comment please?

    Best,
    Majid

    #28090
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Majid,

    Indeed, there is an error in the code, please try this variation:

    add_filter('asp_indexing_string_pre_process', 'custom_chars_asp_indexing_string_pre_process', 10, 1);
    add_filter('asp_search_phrase_before_cleaning', 'custom_chars_asp_indexing_string_pre_process', 10, 1);
    add_filter('asp_query_args', 'custom_chars_asp_indexing_string_pre_process', 10, 1);
    function custom_chars_asp_indexing_string_pre_process($s) {
    	$original = array(
    		'ī', 'ʻ', 'ā', 'Ṭ', 'ṣ', 'ū'
    	);
    	$replace = array(
    		'i', '', 'a', 't', 's', 'u'
    	);
    	
    	// Replace them
    	if ( is_array($s) ) {
    		if ( isset($s['s']) && !$s['_ajax_search'] ) 
    			$s['s'] = str_replace($original, $replace, $s['s']);      
    	} else {
    		$s = str_replace($original, $replace, $s);
    	}
    	
    	return $s;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28092
    msamavi64
    msamavi64
    Participant

    Ernest,

    It seems it works!
    I have added two other special characters, those worked as well 🙂

    I’m very happy now!
    Let me check further.

    Best,
    Majid

    #28093
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    That is really great to hear 🙂 It may not work with everything, but should do most of them.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #28095
    msamavi64
    msamavi64
    Participant

    Hi Ernest,

    One issue regarding small and capital letters (case sensitivity):

    If we use:

    $original = array(
    ‘Ā’, ‘ā’
    );
    $replace = array(
    ‘A’, ‘a’
    );

    Only search string “Ava” will find the post title “Āvā”. How to make it case insensitive so that it can also be found with search strings like: “ava” or “avA” ?

    Regards,
    Majid

    #28096
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Majid,

    Use the lower case for both in the replacement:

    $original = array(
        'Ā', 'ā'
    );
    
    $replace = array(
        'a', 'a'
    );
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Ignoring special characters’ is closed to new replies.