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

phpmb_strwidth(string, [encoding]);

Example

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

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

Code execution result:

php5

Example

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

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

Code execution result:

php10

Example

Let's measure the width of a mixed string:

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

Code execution result:

php9

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