Determining String Inclusion with PHP
You seek a PHP function that evaluates if a given word exists within a provided string. Let's delve into the options available:
$haystack = "I go to school"; $needle = "to"; if (strpos($haystack, $needle) !== false) { echo "Found!" }
if (strstr($haystack, $needle)) { echo "Found!" }
if (preg_match("/{$needle}/", $haystack)) { echo "Found!" }
function match_my_string($needle = 'to', $haystack = 'I go to school') { if (strpos($haystack, $needle) !== false) return true; else return false; } match_my_string($needle, $haystack);
$haystack = "I go to school"; $needle = "to"; if (str_contains($haystack, $needle)) { echo "Found!" }
The above is the detailed content of How do you determine if a string contains a specific word in PHP?. For more information, please follow other related articles on the PHP Chinese website!