mb_strlen() - Counting Multibyte Characters

This section describes how to count multi-byte characters using php_mbstring.dll module.

Once you have configured PHP to use php_mbstring.dll module, you are ready to use multibyte string functions to manipulate Chinese character strings as characters instead of bytes.

Here is simple example PHP script using mb_strlen() to count Chinese characters in a string:

<?php 
#- Count-UTF-8.php
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
#
  $help_simplified = '这是一份非常简单的说明书…';
  $help_traditional = '這是一份非常簡單的說明書…';
  $help_gb18030 = '?????????????';
  $help_big5 = '?????????????';
  
  print('<html>');
  print('<meta http-equiv="Content-Type"'.
    ' content="text/html; charset=utf-8"/>');
  print('<body>');

# Showing UTF-8 characters
  print('<b>Chinese string in UTF-8 in PHP</b><br/>');
  print('UTF-8 simplified characters: '.$help_simplified.'<br/>');
  print('UTF-8 traditional characters: '.$help_traditional.'<br/>');

# Trying to show GB18030 characters
  print('<b>GB18030 string included in a UTF-8 page</b><br/>');
  print('GB18030 characters: '.$help_gb18030.'<br/>');

# Trying to show Big5 characters
  print('<b>Big5 string included in a UTF-8 page</b><br/>');
  print('Big5 characters: '.$help_big5.'<br/>');

# Counting UTF-8 characters
  print('<b>Count UTF-8 characters in strings:</b><br/>');
  print('UTF-8 simplified characters: '
    .mb_strlen($help_simplified).'<br/>');
  print('UTF-8 traditional characters: '
    .mb_strlen($help_traditional).'<br/>');
  print('GB18030 characters: '.mb_strlen($help_gb18030).'<br/>');
  print('Big5 characters: '.mb_strlen($help_big5).'<br/>');

# Counting bytes
  print('<b>Count UTF-8 characters in strings:</b><br/>');
  print('UTF-8 simplified characters: '
    .strlen($help_simplified).'<br/>');
  print('UTF-8 traditional characters: '
    .strlen($help_traditional).'<br/>');
  print('GB18030 characters: '.strlen($help_gb18030).'<br/>');
  print('Big5 characters: '.strlen($help_big5).'<br/>');

  print('</body>');
  print('</html>');
?>

Here is the Web page generated from this PHP script:
Counting UTF-8 Characters and Bytes

Look at the Web page carefully, you will see:

Last update: 2015.

Table of Contents

 About This Book

 PHP Installation on Windows Systems

 Integrating PHP with Apache Web Server

 charset="*" - Encodings on Chinese Web Pages

 Chinese Characters in PHP String Literals

Multibyte String Functions in UTF-8 Encoding

 php_mbstring.dll - Multibyte String Functions

mb_strlen() - Counting Multibyte Characters

 List of Multibyte String Functions

 Input Text Data from Web Forms

 Input Chinese Text Data from Web Forms

 MySQL - Installation on Windows

 MySQL - Connecting PHP to Database

 MySQL - Character Set and Encoding

 MySQL - Sending Non-ASCII Text to MySQL

 Retrieving Chinese Text from Database to Web Pages

 Input Chinese Text Data to MySQL Database

 References

 PDF Printing Version