Mit str_word_count gibt es manchmal Probleme:
PHP
<?php
$text = "Alles schön und gut, aber manchmal funktioniert das nicht";
echo str_word_count($text);
?>
Da liefert das Script eine 10, obwohl 9 richtig wäre.
In den "User Contributed Notes" zu str_word_count findet man in der Doku:
PHP
<?php
function str_word_count_utf8($str)
{
$a = preg_split('/\W+/u', $str, -1, PREG_SPLIT_NO_EMPTY);
return count($a);
}
$text = "Alles schön und gut, aber manchmal funktioniert das nicht";
echo str_word_count_utf8($text);
?>
Alles anzeigen
Damit funktioniert es wieder. Da kann man aber auch gleich explode und count nehmen
