Current Location: Home> Function Categories> mb_strwidth

mb_strwidth

Returns the width of the string
Name:mb_strwidth
Category:Multi-byte string
Programming Language:php
One-line Description:Returns the width of the specified string (in characters), using multibyte character set encoding

Function name: mb_strwidth()

Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7

Function description: The mb_strwidth() function returns the width of the specified string (in characters) and uses multibyte character set encoding.

usage:

 mb_strwidth ( string $str [, string $encoding = mb_internal_encoding() ] ) : int

parameter:

  • $str: The string to calculate the width.
  • $encoding (optional): character encoding of the string. If this parameter is not provided, the internal character encoding returned by the mb_internal_encoding() function is defaulted to use.

Return value:

  • Returns the width in characters of the specified string.

Example:

 $str = "你好,世界!"; $width = mb_strwidth($str, 'UTF-8'); echo "字符串的宽度为:$width"; // 输出:字符串的宽度为:7

In the example above, we use the mb_strwidth() function to calculate the width of a string containing Chinese characters. Since the width of Chinese characters is 2 characters, the result is 7.

Similar Functions
Popular Articles