How to Count Characters in a String in PHP
To count characters in a string in PHP, use the
strlen() function which returns the length of the string including spaces and special characters. For multibyte strings (like UTF-8), use mb_strlen() to get the correct character count.Syntax
The basic syntax to count characters in a string is:
strlen(string $string): int- Returns the number of bytes in the string.mb_strlen(string $string, string $encoding = null): int- Returns the number of characters in a multibyte string, considering encoding.
Note: strlen() counts bytes, so it may not work correctly for multibyte characters like emojis or accented letters.
php
strlen(string $string): int mb_strlen(string $string, string $encoding = null): int
Example
This example shows how to count characters in a simple ASCII string and a UTF-8 string with multibyte characters using strlen() and mb_strlen().
php
<?php // ASCII string $asciiString = "Hello World!"; echo "Length with strlen: " . strlen($asciiString) . "\n"; // UTF-8 string with multibyte characters $utf8String = "Café 😊"; echo "Length with strlen: " . strlen($utf8String) . "\n"; // counts bytes // Use mb_strlen to count characters correctly echo "Length with mb_strlen: " . mb_strlen($utf8String, 'UTF-8') . "\n"; ?>
Output
Length with strlen: 12
Length with strlen: 10
Length with mb_strlen: 6
Common Pitfalls
A common mistake is using strlen() for strings with multibyte characters like emojis or accented letters. strlen() counts bytes, not characters, so it can give a larger number than expected.
Always use mb_strlen() with the correct encoding (usually 'UTF-8') for multibyte strings.
php
<?php // Wrong way: using strlen on multibyte string $str = "😊"; echo strlen($str) . "\n"; // Outputs 4 (bytes), not 1 (character) // Right way: using mb_strlen echo mb_strlen($str, 'UTF-8') . "\n"; // Outputs 1 (character) ?>
Output
4
1
Quick Reference
| Function | Purpose | Notes |
|---|---|---|
| strlen(string $string) | Counts bytes in string | Use for ASCII or single-byte strings |
| mb_strlen(string $string, string $encoding) | Counts characters in multibyte strings | Use 'UTF-8' encoding for most cases |
| mb_internal_encoding() | Gets or sets internal encoding | Useful to set default encoding for mb functions |
Key Takeaways
Use
strlen() to count characters in simple ASCII strings.Use
mb_strlen() with 'UTF-8' encoding for strings with multibyte characters.strlen() counts bytes, which can be different from character count in multibyte strings.Always specify encoding in
mb_strlen() to get accurate results.For most modern applications, prefer
mb_strlen() to handle international text correctly.