How to Check String For Multiple Words in PHP?

03-Apr-2023

.

Admin

How to Check String For Multiple Words in PHP?

Hi Dev,

Now, let's see article of How to check string for multiple words in php. This post will give you simple example of Check if a string contain multiple specific words. if you have question about check if multiple strings exist in another string in php then I will give simple example with solution. This article will give you simple example of php check and return multiple words in string Code Example. Alright, let’s dive into the steps.

Example 1:


index.php

<?php

$searchText = "nicesnippets";

$givenString = "Programming articles listed on nicesnippets belongs to java, python, PHP and more";

if(strpos($givenString, $searchText) !== false) {

echo "nicesnippets text Found!";

} else {

echo "nicesnippets text Not Found!";

}

?>

Output:

nicesnippets text Found!

Example 2:

index.php

<?php

function contains($needles, $haystack) {

return count(array_intersect($needles, explode(" ", preg_replace("/[^A-Za-z0-9' -]/", "", $haystack))));

}

$string = 'site security';

$array = array('building', 'site','security');

$i = contains($array, $string);

echo ($i) ? "Keyword found" : "not found";

?>

Output:

Keyword found

I hope it could help you...

#PHP