function countWordOccurrences($string) {
// Convert the string to lowercase to ensure case-insensitive counting
$string = strtolower($string);
// Remove punctuation and special characters
$string = preg_replace('/[^\w\s]/', '', $string);
// Split the string into an array of words
$words = explode(' ', $string);
// Count the occurrences of each word
$wordCount = array_count_values($words);
return $wordCount;
}
$string = "PHP is a general-purpose scripting language geared " .
"towards web development.[8] It was originally " .
"created by Danish-Canadian programmer " .
"Rasmus Lerdorf in 1993 and released in 1995" .
"The PHP Group now produces the PHP reference implementation";
$result = countWordOccurrences($string);
print_r($result);
/*
run:
Array
(
[php] => 3
[is] => 1
[a] => 1
[generalpurpose] => 1
[scripting] => 1
[language] => 1
[geared] => 1
[towards] => 1
[web] => 1
[development8] => 1
[it] => 1
[was] => 1
[originally] => 1
[created] => 1
[by] => 1
[danishcanadian] => 1
[programmer] => 1
[rasmus] => 1
[lerdorf] => 1
[in] => 2
[1993] => 1
[and] => 1
[released] => 1
[1995the] => 1
[group] => 1
[now] => 1
[produces] => 1
[the] => 1
[reference] => 1
[implementation] => 1
)
*/