PHP search string for string

November 9th 2010

I know this is simple, but I get asked it all the time! Here is how to search a string for another string using PHP.

PHP Code:
<?php
$x 
"brown"// searching for
$y "The quick brown fox, jumped over the lazy dog"// search in

$result strpos($y,$x); // execute the search

if($result=== false) {
    echo 
"The word brown was NOT found in the sentence.";
}
else {
    echo 
"Yes! The word brown was found in the sentence.";
}
?>

Notice, that we searched for the existence of the word "brown" AKA $x in the sentence $y.