What is the difference between using substr_count and preg_match_all in PHP to count occurrences of a word within a string?

When counting occurrences of a word within a string in PHP, substr_count is used to count the number of times a substring appears within a string, while preg_match_all is used with a regular expression pattern to count occurrences of a word within a string. substr_count is more straightforward and efficient when looking for exact matches, while preg_match_all offers more flexibility with pattern matching.

// Using substr_count to count occurrences of a word within a string
$string = "The quick brown fox jumps over the lazy dog";
$word = "the";
$count = substr_count(strtolower($string), strtolower($word));
echo "The word '$word' appears $count times in the string.";

// Using preg_match_all to count occurrences of a word within a string
$string = "The quick brown fox jumps over the lazy dog";
$word = "the";
$count = preg_match_all("/\b$word\b/i", $string, $matches);
echo "The word '$word' appears $count times in the string.";