What are some common methods to search for a specific word within a string in PHP?

When searching for a specific word within a string in PHP, you can use functions like strpos() or preg_match() to check for the presence of the word. The strpos() function returns the position of the first occurrence of a substring within a string, while preg_match() allows for more complex pattern matching using regular expressions.

// Using strpos() to search for a specific word within a string
$string = "This is a sample string";
$word = "sample";

if (strpos($string, $word) !== false) {
    echo "The word '$word' was found in the string.";
} else {
    echo "The word '$word' was not found in the string.";
}

// Using preg_match() to search for a specific word within a string
$string = "This is a sample string";
$word = "sample";

if (preg_match("/\b$word\b/", $string)) {
    echo "The word '$word' was found in the string.";
} else {
    echo "The word '$word' was not found in the string.";
}