PHP – grapheme_strlen() function

A grapheme is the smallest functional unit of a writing system. Graphemes can be interpreted as the smallest units of writing that correspond with sounds.

The grapheme_strlen() function in PHP is used to get the string length in grapheme units. This function does not get the byte or character's length but counts the actual displayed characters, making it useful for Unicode text with combining characters. The grapheme_strlen function is supported in PHP 5.3.0 and higher versions.

Note: The Intl extension must be installed and enabled to use this function.

Syntax

int grapheme_strlen(string $input)

Parameters

grapheme_strlen() accepts only one parameter −

  • $input − The string parameter used to measure the length. It must be a UTF-8 encoded string.

Return Value

This function returns the length of the string in grapheme units on success, or false on failure.

Example 1: Basic Usage

Here's a simple example showing how grapheme_strlen() differs from strlen()

<?php
   $string = "café";
   
   echo "String: " . $string . "<br>";
   echo "grapheme_strlen(): " . grapheme_strlen($string) . "<br>";
   echo "strlen(): " . strlen($string) . "<br>";
?>
String: café
grapheme_strlen(): 4
strlen(): 5

Example 2: Unicode with Combining Characters

This example demonstrates the function with Unicode combining characters −

<?php
   // String with combining characters
   $string = "a\xCC\x8Ao\xCC\x88";  // å and ö with combining marks
   
   echo "grapheme_strlen(): " . grapheme_strlen($string) . "<br>";
   echo "strlen(): " . strlen($string) . "<br>";
   echo "mb_strlen(): " . mb_strlen($string, 'UTF-8') . "<br>";
?>
grapheme_strlen(): 2
strlen(): 6
mb_strlen(): 4

Comparison

Function Measures Unicode-aware Grapheme-aware
strlen() Bytes No No
mb_strlen() Characters Yes No
grapheme_strlen() Graphemes Yes Yes

Conclusion

The grapheme_strlen() function provides accurate length measurement for Unicode text by counting graphemes instead of bytes or individual Unicode code points. This makes it ideal for multilingual applications where proper text handling is crucial.

Updated on: 2026-03-15T10:03:29+05:30

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements