PHP 8.3.4 Released!

grapheme_strlen

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

grapheme_strlenLit la taille d'une chaîne en nombre de graphème

Description

Style procédural

grapheme_strlen(string $string): int|false|null

Lit la taille d'une chaîne en nombre de graphème (et non pas en octets ou caractères).

Liste de paramètres

string

La chaîne à mesurer. Elle doit être une chaîne UTF-8 valide.

Valeurs de retour

La taille de la chaîne en cas de succès, ou false si une erreur survient.

Exemples

Exemple #1 Exemple avec grapheme_strlen()

<?php

$char_a_ring_nfd
= "a\xCC\x8A"; // 'LATIN SMALL LETTER A WITH RING ABOVE' (U+00E5) forme normalisée "D"
$char_o_diaeresis_nfd = "o\xCC\x88"; // 'LATIN SMALL LETTER O WITH DIAERESIS' (U+00F6) forme normalisée "D"

print grapheme_strlen( 'abc' . $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_a_ring_nfd);

?>

L'exemple ci-dessus va afficher :

6

Voir aussi

add a note

User Contributed Notes 1 note

up
7
jonhoggg at gmail dot com
2 years ago
You can use grapheme_strlen() to count emojis as a single character:

<?php
echo strlen('🏴󠁧󠁢󠁥󠁮󠁧󠁿'); //28 - oh no
echo mb_strlen('🏴󠁧󠁢󠁥󠁮󠁧󠁿'); // - closer, but no
echo grapheme_strlen('🏴󠁧󠁢󠁥󠁮󠁧󠁿'); //1 - Bingo
?>
To Top