Hi there,

I need You.

Here is my request:

I have a mysql database and I am doing some query on it, using the Fulltext function and the boolean one.

What I want to do is to have the ability to find a singular word when the plurial of the word is existing. And the opposite too.

For example: the query "cat" could find "cat" or "cats"
and the query "cats" could find "cat".

I kno that I can use "word*", but I am doing this for my customer who does'nt know all the time the way of...

How can I do that?

Sincerely yours,

Marc-André 😕

    Maybe this is kind of kludgey, but you could look for an 's' or 'es' as the last letter/s, splice it/them off, and then do the search with the resulting string ... most plurals in English end in 's' or 'es' - and non plurals with these endings won't suffer much: rabbits would become rabbit, and horses would become hors, while class would become clas, and rabies would be come rabi.

      Thank you but how do I do that?

      My search string could be:
      "a black cat on a roof with bubbles"

      How to I do to change the characters of cat to "cat" or bubbles to "bubbl", for example. I have looked inside the php manual, but I dn't know how to find the last letter(s) in a query with several words...

      MArc-André 🙁

        Use explode() on the string:

        $words = explode(' ', $string);
        

        You now have an array, $wqords, with values that hold one word each

        Then use a regex to see if the last letter is an 'es' or 's':

        foreach ($words as $word) {
            if ($string = eregi_replace('es$', '', $word) || $string = eregi_replace('s$', '', $word))
               $newWords[] = $string;
        }
        

        Now you have an array, $newWords, in which all values are now '-s' and '-es' free.

        You may also want to assemble a list of extremely common words, and remove them from the array. Do this before the regex loop.

          You are great!

          But I am a novice in PHP.

          It seems that your code put a result in an array -$newWords[]-

          How do I retransform this in my variable $string?

          MArc-André

            I have made several tries, but nothing done...

            When I try to use $string or $newWords, I get for answer "1" or "11".

            What's hap?

            Please help,

            MArc

              I have made some tests and your syntax take olny the end of the line ('es$'). And I have tested with several words like:

              query = "black cats bubbles", and it change only bubbles to bubbl.

              Is there a solution?

              Marc-André 😕

                Of course it does ... if you take off the part that removes single s's, then only the es's will be removed, because the two are not the same.

                As for getting the value out of the array again just build yourself a string while you're in the foreach loop:

                foreach ($words as $word) {
                    if ($string = eregi_replace('es$', '', $word) || $string = eregi_replace('s$', '', $word))
                        $newWords[] = $string;
                        if (!$newString)
                             $newString = $string;
                        else
                            $newString .= ", $string";
                }
                

                Now you have a comma separated list of words, none of which are plural.

                  I have the solution:

                  here is the correction applying to the code from Steadyguy:
                  the variable $search contains for example: "cats bubbles"

                  $words = explode(' ', $search);
                  foreach ($words as $word) { 
                      $string_one = eregi_replace('es$', '*', $word);
                      $string = eregi_replace('s$', '*', $string_one);
                      $newWords[] = $string; 
                  }
                  
                  for ($i=0; $i < count ($newWords); $i++)
                      {
                      $spacer = " ";
                      $string .= $spacer. $newWords[$i];
                      }
                  $search = $string;
                  

                  Thanks for all,

                  Marc-André

                    Its a good gesture to mark the thread "resolved" also.

                      I'm the only that's been responding, here ... but ... you're welcome.

                      You can make it faster and easier to read if you don't use the for loop, just do it inside the foreach:

                      $words = explode(' ', $search);
                      foreach ($words as $word) { 
                          $string_one = eregi_replace('es$', '*', $word);
                          $string = eregi_replace('s$', '*', $string_one);
                          //you do realize that this will remove an extra s from words, right? ... 'classes' ...  will end up as 'clas*'
                          $newWords[] = $string; 
                          $search .= " $string";
                      }
                      

                      If you don't want to do that, at least take the count($newWords) out of the loop conditions, and replace it with a variable - aside from being good coding practice, this will speed up your code, because it doesn't have to count the number of elments in your array every time, just once.

                        you perfectly right, I am gonna test your code...

                        Marc😉

                          Thanks again for your help Steadyguy.

                          Here is what I have learned testing your code and mine:

                          I have done this:

                          $string_one = eregi_replace('es$', '*', $word); 
                          $string = eregi_replace('s$', '*', $string_one);
                          

                          Because, first, it looks at the end finshing by 'es' and AFTER he could try to find 's' at the end.
                          For the case "classes", it is fully functionnal, because the first line return "class" and then does not math the second line. That's why I have had "" for the replacement and not nothing '' as you propose.

                          I have done all the test upside down, and it's works correctly.

                          For the counting code, I agree, but my query return twice the entry, for example the query "cats bubbles" is returning with your code: "cats bubbles cat bubbl", that's why I decided to do the count. I know it is not perhaps the best way, but I am a PHP novice.....

                          MArc-André 😉

                            Finally, it's not a good way and a good Idea to do that inside a Mysql database. After compulsing the dictionary, it's very complicate to try managing the plural of english words....

                            I think, it is better to call a class or something, but I don't know how to do that....

                            It's easier, I think, to include the singular and the plural words, inside the database.

                            Thank you again for your time and your genius....
                            Marc-André 🙂

                              Write a Reply...