Search code examples
phpwordpressfor-loopstrpos

Compare 2 strings in PHP


I'm in trouble because i can't figured it out how to find my answer. I'm creating a wordpress website and i have a search engine

I've created an option in my admin panel and code something to write in my option table, but my problem is now

firstly i grab my options from my database with

$mysynonymvalue = get_option( 'synonym-custom' );

I precise that it returne to me something like this ( mango, apple, banana)(this is an example of course)

My Url is something like this :

http://supserwebsite/wordpress/?sfid=2675&_sf_s=toto

Or this

http://superwebsite/wordpress/?sfid=2675&_sf_s=virtualisation cloud devops

so i've created a variable to catch the queries

$motsclefs3= $_GET['_sf_s'];

Now i want to compare the string $mysynonymvalueconvert with $motsclefs3 to find if it match so i write

                            if (strpos ($mysynonymvalue, $motsclefs3) ){
                            echo '<script >
                                $(document).ready(function(){
                                    $(".vc-tabs-li:nth-child(2)").get(0).click();
                                });
                            </script>';
                            }                           
                            else{
                                echo '
                        <script >
                                $(document).ready(function(){
                                    $(".vc-tabs-li").get(0).click();
                                });
                            </script>';

                            };
                        };                  

The solution seem to work correctly but i can't have the first result, it coompare indeed with all the results but not my first one.

And it doesn't work so fine because with only one letter it return a match ( for example 'a')

Any solution ? Thanks


Solution

  • So far i move up a bit !So i came with this

    I still have

    $mysynonymvalue = get_option( 'synonym-custom' );
    $mysynonymvalueconvert = preg_split('/[,]+/',$mysynonymvalue);
    

    To grab my words from my database and to convert it into an array. (the point of this is to get elements that were wrote by an user elsewhere on the admin panel of wordpress)

    I also still have

     $motsclefs3= $_GET['_sf_s'];
    

    To grab my actual queries ( that will serv me to compare ). I precise that it return me a string. To be more specific, an URL like this (http://mywebsite/wordpress/?sfid=2675&_sf_s=examen) return me (in string) "examen".

    Now my point is still to compare if

      $motsclefs3;
    

    is inside

     $mysynonymvalueconvert
    

    So i've created a "for" loop like this

    for ($i = 0;  $i <= count($mysynonymvalueconvert); $i++){                                                    
                                    if(in_array($motsclefs3, $mysynonymvalueconvert)){                                 
                                        echo'yes';
                                        break;
                                   }
                                   else{
                                        echo 'no';
                                        break;
                                   };
                                };
    

    But i'm still blocked, this return "yes" only if it match with the first element from

     $mysynonymvalueconvert
    

    So any ideas to help me ?

    Thanks!