nehmen Ihre $ cResult als Eingang:
$word_counts = [];
// remove scripts and styles completely, then strip tags
$cResult = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $cResult);
$cResult = preg_replace('#<style(.*?)>(.*?)</style>#is', '', $cResult);
$cResult = strip_tags($cResult);
// strip all characters that are not letters:
$word_array_raw = explode(' ',preg_replace('/[^A-Za-z ]/', ' ', $cResult));
// loop through array:
foreach ($word_array_raw as $word) {
$word = trim($word);
if($word) {
isset($word_counts[$word]) ? $word_counts[$word]++ : $word_counts[$word] = 1;
}
}
// Array with all stats sorted in descending order:
arsort($word_counts);
// Output format you wanted:
foreach ($word_counts as $word=>$count) {
echo "$word | $count<br>";
}
Hoffe, es hilft
Ich bin nicht sicher, was Ihr Code hat mit Ihrer Frage zu tun, aber man könnte versuchen, [ 'str_split()'] (http://php.net/manual/en/function.str-split.php) um es auf Leerzeichen zu teilen, dann über das Array zu schleifen und die Wörter als Schlüssel in einem anderen Array zu verwenden, jedes Mal inkrementierend. Wenn Sie sich an einer beliebigen Wortgrenze aufteilen wollen, verwenden Sie ['preg_split()'] (http://php.net/manual/en/function.str-split.php) und teilen Sie sie auf '\ b'. Direkt von meinem Kopf. – alanlittle