1

I got lots of result and codes about search string. But all are search single word. I want to search multiple word and any of word match string will be display.

Example:

$string = "Apple is a big tech company!";
$search = 'Apple Logo';

Now I want if "Apple Logo" or "Apple" or "Logo" in string then it will return True else it will show False.

How I do that? I already tried lot of PHP codes. I also see ElasticSearch but I want something handy and easy to use.

if (stripos($string, $search) !== false) {
 echo "Found";
}
5
  • "I also see ElasticSearch but I want something handy and easy to use." - Where does the text you want to search come from? Database? Text file? Hard coded? Going from regex on a string to ElasticSearch is a pretty wide jump. Commented Apr 18, 2018 at 18:14
  • Actually, I'll use this function to search inside a multidimensional array.. so my one friend suggest me to use ElasticSearch.. But I don't want to use a third party tools. Commented Apr 18, 2018 at 18:23
  • You don't show any multidimensional array which may be a different answer entirely. Commented Apr 18, 2018 at 18:24
  • yeah.. I didn't add it here.. because I want a simple answer.. I posted another question about this and didn't get a answer, that's why I posted this question more simple way. Commented Apr 18, 2018 at 18:26
  • ...and where do you get that multidimensional array from? Regardless if it is in a string or if it is in an array, it comes from somewhere at some point, right? Database? Text file? Hard coded? The answer depends on that as well since you already can search for the text if you have it in a database. Commented Apr 18, 2018 at 18:27

3 Answers 3

6

You can use an array of the words, replace them and check against the original:

if(str_ireplace(explode(' ', $search), '', $string) != $string) {
    echo "Found";
}

Or loop the words and check as you would with a single word:

foreach(explode(' ', $search) as $word) {
    if(stripos($string, $word) !== false) {
        echo "Found";
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would use preg_grep and some other fancy things.

$string = "Apple is a big tech company!";
$search = 'Apple Logo a';

$pattern = '/\b('.str_replace(' ', '|', preg_quote($search, '/')).')\b/i';

$arr = preg_grep($pattern, explode(' ', $string));

print_r($arr);

Outputs

Array ( [0] => Apple [2] => a )

Test it online

https://3v4l.org/ugbdZ

I threw the a in there just to show off. As you can see it only matches a not company etc..

And as a bonus it will properly escape any Regex stuff in the search string...

Yay!

As a side note you could also use the same pattern with preg_match_all if you wish.

$string = "Apple is a big tech company!";
$search = 'Apple Logo a';

$pattern = '/\b('.str_replace(' ', '|', preg_quote($search, '/')).')\b/i';

$numMatch = preg_match_all($pattern,$string,$matches);

print_r($numMatch);
print_r($matches);

Outputs

 2
 Array (
      [0] => Array (
           [0] => Apple
           [1] => a
      )
      [1] => Array (
           [0] => Apple
           [1] => a 
      )
 )

Test it

https://3v4l.org/ZOlUV

The only real difference is you get a more complicated array (just use $matches[1]) and the count of the matches without counting said array.

Comments

-3

you can use this STRPOS example:

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

this a little example.. good luck

1 Comment

You're on the English SO. All answers should be in English. Plus, it doesn't answer the OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.