CKTIS / PHP
mb_strlen()
La fonction mb_strlen compte les caractères dans l’encodage indiqué, null sélectionne l’encodage interne.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
mb_strlen(string $string, ?string $encoding = null): intLa fonction mb_strlen compte les caractères dans l’encodage indiqué, null sélectionne l’encodage interne. En UTF-8 elle compte les points de code, donc une marque combinante et sa base comptent séparément même pour une lettre visible. Une chaîne vide compte zéro. Un encodage inconnu lève ValueError en PHP 8.4.
Signature
mb_strlen(string $string, ?string $encoding = null): int
Sortie de l’exemple
[2,3,1,0]
ValueError
Exemples
<?php
$s = "e\u{0301}";
echo json_encode([mb_strlen($s, "UTF-8"), strlen($s), mb_strlen("é", "UTF-8"), mb_strlen("", "UTF-8")]), PHP_EOL;
try { mb_strlen("x", "not_an_encoding"); } catch (ValueError $e) { echo get_class($e); }
Résultat
[2,3,1,0] ValueError