Horje
How to check if a string contain multiple specific words

substr_count() is counting for how many times the string appears, so when it's 0 then you know that it was not found. I would say this way is more readable than using str_pos()


If match some word in php

index.php
Example: PHP
<?php
$data = "bangla";
if (strpos($data, 'bangla')!==false || strpos($data, 'english')!== false) { echo "Found";
} else { echo "Not Found";
}
?>

Output should be:

If match some word in php

PHP Match Specific Word

Here is a function that can perform this operation without using regular expressions which could be slower. Instead of passing a single string for the task, pass an array like
index.php
Example: PHP
<?php
// function
function strposMultiple($haystack, $needle, $offset = 0) { if(is_string($needle)) return strpos($haystack, $needle, $offset); else { $min = false; foreach($needle as $n) { $pos = strpos($haystack, $n, $offset); if($min === false || $pos < $min) { $min = $pos; } } return $min; }
}
// PHP Code for Result
$data = 'bad';
if (strposMultiple($data, ['bad', 'naughty']) !== false) { echo 'Matched';
} else { echo 'Not Matched';
}
?>

Output should be:

PHP Match Specific Word

If Match Some Word

For this, you will need Regular Expressions and the preg_match function. Something like:

index.php
Example: PHP
<?php
$data = 'bad';
if(preg_match('(bad|naughty)', $data) === 1)
{
echo 'Yes';
} else {
echo 'No';
}
?>

Output should be:

If Match Some Word

If Multiple Word Match

(preg_match return 1 if there is a match in the string, 0 otherwise).

multiple str_pos calls

index.php
Example: PHP
<?php
$data = 'bad';
if (strpos($data, 'bad')!==false or strpos($data, 'naughty')!== false) { echo "Found";
} else { echo "Not Found";
}
?>

Output should be:

If Multiple Word Match





Category :
Web Tutorial
Sub Category :
PHP String Tutorial
Uploaded by :
Admin


Read Article
https://horje.com/learn/1434/reference