0

I'm using preg_match to check if a string has a specific word in it:

$string = 'This string contains a few words that are in an array!';
$arr = ['string', 'few', 'words'];

if(preg_match('['.implode('|', $arr).']', $string)){
    //do something if there is at least one word from the array inside that string
}

This is working quite nice but it will return true if there is at least one word and i need it to return true if there are at least two words from the array inside that string.

Can that be done in one step? If not, which way should i go from here to obtain that result?

Thank you!:D

4
  • 1
    Try preg_match('~\b(?:'.implode('|', $arr).')\b.*\b(?:'.implode('|', $arr).')\b~', $string) Commented Dec 11, 2018 at 23:12
  • 1
    Try substr_count php.net/manual/en/function.substr-count.php Commented Dec 11, 2018 at 23:12
  • hey @MA, but how do i use substr_count for multiple needles(and case insensitive) :-s. For example i want to check string: This is mine and yours for arr: is, and so if is and and are found in that string it will return true :-s Commented Dec 11, 2018 at 23:20
  • hey @WiktorStribiżew can you please help me understand what is going on there? :-s Commented Dec 11, 2018 at 23:20

3 Answers 3

2

If your requirement is to know that at least 2 of the required words exist in the string then you need to be careful. If you have 2 of the same words in the string it is easy to get a false positive if you just use preg_match_all to search for occurances.

This will report 3 i.e. all 3 words are present in the haystack

$string = 'This string contains a string and a few other words!';
$finds = ['string', 'few', 'words'];
$findCount = 0;

foreach ($finds as $find) {
    if ( strpos($string, $find) !== false ) $findCount++;
}
echo $findCount;

It will report 2 if you use this string

$string = 'This string contains a string and a other words!';

And most importantly report only ONE if you use this string which contains the word string twice, but not 2 of the required words.

$string = 'This string contains a string and other stuff!';
Sign up to request clarification or add additional context in comments.

Comments

0

You can use preg_match_all for this

$string = 'This string contains a few words that are in an array!';
$arr = ['string', 'few', 'words'];
$count = preg_match_all('['.implode('|', $arr).']', $string); // Returns 3

1 Comment

Except that if the string contains 'This string contains a string' it will also return 2 as there are 2 string words. But it does not contain at least 2 of the words you are looking for it just finds string twice
0

The selected answer is fine if you don't care exact match. If you want to match EXACT word then you need to use word boundary \b.

Here is the example:

$arr = ['string', 'few', 'words'];

preg_match_all('#\b('.implode('|', $arr).')\b#', $string, $wordsFound);

$wordsFound = array_unique($wordsFound[0]);

if(count($wordsFound) >= 2){
    echo "Matched";
}else{
    echo "Not matched";
}


Input: $string = 'This string contains a few words that are in an array!';
Output: Matched (3 founds)

Input: $string = 'This string222 contains a few words that are in an array!';
Output: Matched (2 founds)

Input: $string = 'This string222 contains a few23 words that are in an array!';
Output: Not Matched (1 found)

Input: $string = 'This string contains a string words that are in an array!';
Output: Matched (2 founds)

Input: $string = 'This string contains a string string that are in an array!';
Output: Not Matched (1 found)

2 Comments

This has a probelm if the string is A string and a string
@RiggsFolly code updated. Now it will check unique value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.