mb_strlen
Returns the number of characters in a string.
Available since: PHP 4
Syntax
mb_strlen(string $string, ?string $encoding = null): intParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The string to measure. |
| $encoding | ?string | The encoding. Defaults to the internal encoding, usually UTF-8. |
Return value
intThe number of characters in the string.
Examples
Length is counted in characters whatever the language:
<?php
echo mb_strlen('привет'), "\n";
echo mb_strlen('hello');Output:
6
5Checking a length limit:
<?php
$name = 'Александр';
var_dump(mb_strlen($name) <= 10);Output:
bool(true)Notes
Use mb_strlen() for any user-entered text: plain strlen() returns bytes and counts Cyrillic text as twice as long.
See also
Related recipes
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.