How can the substr_count() function be optimized for better performance when counting word frequency in PHP?

When using substr_count() to count word frequency in PHP, the function can be optimized for better performance by breaking the input string into an array of words and then using array_count_values() to count the frequency of each word. This approach avoids multiple iterations over the input string and is more efficient for large texts.

$input_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

$words = str_word_count($input_string, 1);
$word_frequency = array_count_values($words);

print_r($word_frequency);