53 of 410 menu

The mb_strwidth Function

The mb_strwidth function measures the width of a string, considering that full-width characters (for example, Chinese characters) occupy 2 positions, while regular characters occupy 1 position. The first parameter is the string to be measured, the second optional parameter is the encoding.

Syntax

mb_strwidth(string, [encoding]);

Example

Let's measure the width of a string with Latin characters:

<?php echo mb_strwidth('Hello'); ?>

Code execution result:

5

Example

Let's measure the width of a string with Japanese characters:

<?php echo mb_strwidth('こんにちは'); ?>

Code execution result:

10

Example

Let's measure the width of a mixed string:

<?php echo mb_strwidth('Hello 世界'); ?>

Code execution result:

9

See Also

  • the mb_strlen function,
    which returns the length of a string in characters
  • the strlen function,
    which returns the length of a string in bytes
byenru