How to Check a string contains a specific word in PHP (with Multiple Answers)

PHP have many functions to find the occurrence of word.

strpos()

$x = 'Where are you?';

if (strpos($x, 'are') !== false) { echo 'specific Word exists'; }

mb_stpos()

if (mb_strpos($x, 'are') !== false) { echo 'specific Word exists'; }

stripos()

$stringresult = stripos($x, 'are');

if ($stringresult === false) { echo 'specific Word does not exist';
} else { echo 'specific Word exists'; }

Count Number of Times a Word Found.

substr_count()

$oritext = 'This is a Book';
echo substr_count($oritext, 'is'); // return 2

if (substr_count($oritext, 'is') > 0) { echo "is exists"; }

 

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *