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...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example