How can regular expressions be used to search for a word within a string in PHP?

Regular expressions can be used in PHP to search for a specific word within a string by using the preg_match function. This function takes a regular expression pattern and a string to search within, and returns true if the pattern is found in the string. To search for a specific word, the regular expression pattern should include the word surrounded by word boundaries (\b) to ensure an exact match.

$string = "The quick brown fox jumps over the lazy dog";
$word = "fox";

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.";
}