Search bar to ignore spaces

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Search bar to ignore spaces

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #33652
    alisonhanson26
    alisonhanson26
    Participant

    Hi,

    I would like my search bar to ignore spaces. For example, I would like 2020 Case 1 and 2020Case1 to return the same search results, is this possible? If so, how do I do it?

    Thanks!

    -Alison

    #33660
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Well, with a simple custom code it is possible to remove the spaces from the phrase, so the search term “2020 Case 1” becomes “2020Case1”. Based on this knowledge base, the code is:

    add_filter('asp_search_phrase_before_cleaning', 'asp_replace_characters', 10, 1);
    add_filter('asp_query_args', 'asp_replace_characters', 10, 1);
    
    function asp_replace_characters( $s ) {
      $characters = " "; // Type characters one after another
      $replace_with = '';     // Replace them with this (space by default)
    
      if ( is_array($s) ) {
        if ( isset($s['s']) && !$s['_ajax_search'] ) 
          $s['s'] = str_replace(str_split($characters), $replace_with, $s['s']);      
      } else {
        $s = str_replace(str_split($characters), $replace_with, $s);
      }
    
      return $s; 
    }
    Best,
    Ernest Marcinko

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


    #33674
    alisonhanson26
    alisonhanson26
    Participant

    Hi,

    I added that custom code via the code snippets plugin but it is not working. I would like my users to search 2019 Case 1 or 2019Case1 and get the same results.

    Thansk!

    -Alison

    #33675
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Alison,

    In this specific case, does the keyword “2019Case1” exist somewhere in the post content or in the selected fields? If it does not exist, then this is not going to be possible.
    All that code does is removes the spaces from the phrases entered, and then matches the resulting keyword agains the selected fields.

    I think what you mean is somehow detecting when the user did not enter the spaces and account for them – basically the other way around. Unfortunately that is not possible. There is no way to tell from the code perspective where the spaces should be, and trying all the variations would simply overload the server within seconds.

    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.