J'expérimente quelque chose où explode() échoue, donc je veux essayer autre chose. Si j'ai une chaîne, comment puis-je compter le nombre de caractères dans ce qui disons un comma
comme dans ,
Comptage du nombre de caractères X
1
A
Répondre
3
Essayez substr_count():
substr_count($text, ',');
1
Vous pouvez utiliser count_chars
Exemple de Manuel PHP:
$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
sortie (codepad):
There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string.
+0
N'oubliez pas de vérifier 'isset ($ result [', ']) 's'il n'y a pas de garantie, une virgule est dans la chaîne (possibilité de ne pas avoir d'élément virgule dans le tableau résultat) –
Vous voulez ** str_word_count ** http://us3.php.net/manual/fr/function.str-word-count.php – Jakub