How to find 2 or more words in a string with preg_match?

hay there :smiley:

How can i check for say 2 or more words in a string…

Now I know this checks for 2 words on a “OR” basis

[php]

if(preg_match("/$findthis|$this/i", $text_to_search))
{

//do something

}

[/php]

So this ill check to see if the [php]$text_to_search[/php] contains either[php] $findthis [/php] or [php]$this[/php]
but i need it to check with the “and” operator - so for example…

search [php]$text_to_search[/php] for [php]$findthis[/php] and $this

how can i do this?

many thanks :slight_smile:

Johnd123,

Here’s the correct way to do it, you need to make sure you preg_quote all your search terms or it will throw off the preg_match and you will get invalid results on some search queries.

[php]function matchWords($findtheseItems , $text_to_search)
{
$items = explode(" ", $findtheseItems);

 foreach ($items as $item)
{
     if (preg_match('/'. preg_quote($item) .'/i',$text_to_search) == false)
       {
          return false;
       }
}

return true;
}[/php]

Ahhh man thanks so much for helping :slight_smile:

this is perfect!

bless you sir!

Maybe more correct since you don’t need regex:

[php]if(stripos($text_to_search, $findthis) !== false && stripos($text_to_search, $findthis2) !== false)[/php]

Or you could add it in a little function.

cheers but i dont think this will help my needs as the number of words will depend on user submitted… like a search engine…

So far the best method that works well is this

[php]

function SplitQuery($this_query)
{
$all_words = explode(" “, $this_query); // Get each word from query
foreach($all_words as $val => $word) // Loop through each word
{
$toreturn = $toreturn . $word.”.+."; // Add ‘.+.’ for preg_match
}
$preg_stuff = trim($toreturn, “.+.”); // trim of the end
return $preg_stuff; //Return all words example: word1.+.word2.+.word3
}

[/php]

then

[php]

if(preg_match("/".SplitQuery($query)."/i", $content_to_search)) // ----- /each.+.word.+.entered/i
{
//do something
}

[/php]

thanks for helping :slight_smile:

If you put @AbraCadaver solution in a function it will look like this, you’ll need to run tests to see which is faster for your use.

[php]function matchWords($findtheseItems, $text_to_search)
{
$items = explode(" ", $findtheseItems);

 foreach ($items as $item)
{
     if(stripos($text_to_search, $item) == false)
       {
          return false;
       }
}

return true;
}[/php]

=== false

groovy guys ill give them a baz :smiley:

thanks so much for help… much appreciated :slight_smile:

if(preg_match("/".SplitQuery($query)."/i", $content_to_search)) // ----- /each.+.word.+.entered/i { //do something }

You can’t do that without the preg_quote, because if your search term contains and of these characters you won’t get back good results.

. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

Thanks for the correction @AbraCadaver

Below is your best option if you don’t need to regex something it will be faster.

[php]function matchWords($findtheseItems, $text_to_search)
{
$items = explode(" ", $findtheseItems);

 foreach ($items as $item)
{
     if(stripos($text_to_search, $item) === false)
       {
          return false;
       }
}

return true;
}[/php]

this also works well, nice to have so many different ways of doin this… i guess its just down to speed now but i have no idea which is fast as they all work very well - i think i read that stripos was faster…
in need to read up on stripos tbh lol

ahh i see that would make sense - ill use that one then :slight_smile:

cheers again guys

Sponsor our Newsletter | Privacy Policy | Terms of Service