mb_strwidth()
mb_strwidth() — Return width of string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns the width of string string, where halfwidth characters count as 1, and fullwidth characters count as 2. See https://www.unicode.org/reports/tr11/ for details regarding East Asian character widths.
Signature
mb_strwidth(string $string, ?string $encoding = null): int
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $string | string | The string being decoded. |
| $encoding | ?string | The encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used. |
Return value
The width of string string.
Example
A runnable snippet:
var_dump( mb_strwidth('a'), // LATIN SMALL LETTER A mb_strwidth("\u{ff41}") // FULLWIDTH LATIN SMALL LETTER A );
Output:
int(1) int(2)
Changelog
| Version | Change |
|---|---|
| 8.0.0 | encoding is nullable now. |
| 8.0.0 | Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned. |
See also
mb_strimwidth(), mb_internal_encoding()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page mb_strwidth() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.