PHP Tutorials - Herong's Tutorial Examples - Version 4.20, by Dr. Herong Yang
Examples of Using "mbstring" Functions
This section provides a tutorial example of using mbstring functions, mb_get_info(), mb_detect_encoding(), mb_strlen(), mb_convert_encoding(), etc.
I wrote the following script to test some of "mbstring" basic functions, MbStringBasic.php:
<?php # MbStringBasic.php #- Copyright (c) 2007-2019, HerongYang.com, All Rights Reserved. # print("\nCurrent settings:\n"); $settings = mb_get_info(); foreach ($settings as $k => $v) { print " $k = ($v)\n"; } print("\nEncoding detection:\n"); $str = "Hello!"; $coding = mb_detect_encoding($str); print("1. ".$coding." for (\x".bin2hex($str).")\n"); $str = "\x00H\x00e\x00l\x00l\x00o\x00!"; $coding = mb_detect_encoding($str); print("2. ".$coding." for (\x".bin2hex($str).")\n"); $str = "\xC2\xA1Hola!"; $coding = mb_detect_encoding($str); print("3. ".$coding." for (\x".bin2hex($str).")\n"); $str = "\xE4\xBD\xA0\xE5\xA5\xBD!"; $coding = mb_detect_encoding($str); print("4. ".$coding." for (\x".bin2hex($str).")\n"); $str = "\xC4\xE3\xBA\xC3\xA3\xA1"; $coding = mb_detect_encoding($str); print("5. ".$coding." for (\x".bin2hex($str).")\n"); print("\nString length:\n"); $str = "Hello!"; $length = mb_strlen($str, "ASCII"); print("1. ".$length." for (\x".bin2hex($str).")\n"); $str = "\x00H\x00e\x00l\x00l\x00o\x00!"; $length = mb_strlen($str, "UTF-16"); print("2. ".$length." for (\x".bin2hex($str).")\n"); $str = "\xC2\xA1Hola!"; $length = mb_strlen($str, "UTF-8"); print("3. ".$length." for (\x".bin2hex($str).")\n"); $str = "\xE4\xBD\xA0\xE5\xA5\xBD!"; $length = mb_strlen($str, "UTF-8"); print("4. ".$length." for (\x".bin2hex($str).")\n"); $str = "\xC4\xE3\xBA\xC3\xA3\xA1"; $length = mb_strlen($str, "GB2312"); print("5. ".$length." for (\x".bin2hex($str).")\n"); print("\nString conversion - ASCII <--> UTF-16:\n"); $str = "Hello!"; print(" String in ASCII = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "UTF-16", "ASCII"); print(" Converted to UTF-16 = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "ASCII", "UTF-16"); print(" Converted to ASCII = (\x".bin2hex($str).")\n"); print("\nString conversion - UTF-8 <--> UTF-16:\n"); $str = "\xC2\xA1Hola!"; print(" String in UTF-8 = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "UTF-16", "UTF-8"); print(" Converted to UTF-16 = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "UTF-8", "UTF-16"); print(" Converted to UTF-8 = (\x".bin2hex($str).")\n"); print("\nString conversion - UTF-8 <--> GB2312:\n"); $str = "\xE4\xBD\xA0\xE5\xA5\xBD!"; print(" String in UTF-8 = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "GB2312", "UTF-8"); print(" Converted to GB2312 = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "UTF-8", "GB2312"); print(" Converted to UTF-8 = (\x".bin2hex($str).")\n"); print("\nString conversion - GB2312 <--> UTF-16:\n"); $str = "\xC4\xE3\xBA\xC3\xA3\xA1"; print(" String in GB2312 = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "UTF-16", "GB2312"); print(" Converted to UTF-16 = (\x".bin2hex($str).")\n"); $str = mb_convert_encoding($str, "GB2312", "UTF-16"); print(" Converted to GB2312 = (\x".bin2hex($str).")\n"); ?>
I you run it directly, you will get:
Current settings: internal_encoding = (UTF-8) http_input = () http_output = (pass) func_overload = (pass) Encoding detection: 1. ASCII for (\x48656c6c6f21) 2. ASCII for (\x00480065006c006c006f0021) 3. UTF-8 for (\xc2a1486f6c6121) 4. UTF-8 for (\xe4bda0e5a5bd21) 5. UTF-8 for (\xc4e3bac3a3a1) String length: 1. 6 for (\x48656c6c6f21) 2. 6 for (\x00480065006c006c006f0021) 3. 6 for (\xc2a1486f6c6121) 4. 3 for (\xe4bda0e5a5bd21) 5. 3 for (\xc4e3bac3a3a1) String conversion - ASCII <--> UTF-16: String in ASCII = (\x48656c6c6f21) Converted to UTF-16 = (\x00480065006c006c006f0021) Converted to ASCII = (\x48656c6c6f21) String conversion - UTF-8 <--> UTF-16: String in UTF-8 = (\xc2a1486f6c6121) Converted to UTF-16 = (\x00a10048006f006c00610021) Converted to UTF-8 = (\xc2a1486f6c6121) String conversion - UTF-8 <--> GB2312: String in UTF-8 = (\xe4bda0e5a5bd21) Converted to GB2312 = (\xc4e3bac321) Converted to UTF-8 = (\xe4bda0e5a5bd21) String conversion - GB2312 <--> UTF-16: String in GB2312 = (\xc4e3bac3a3a1) Converted to UTF-16 = (\x4f60597dff01) Converted to GB2312 = (\xc4e3bac3a3a1)
Some interesting notes about this test:
Last update: 2019.
Table of Contents
Introduction and Installation of PHP 7.3
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
►"mbstring" Extension and Non-ASCII Encoding Management
"mbstring" - Multi-Byte String Extension
mb_convert_encoding() and Other mbstring Functions
►Examples of Using "mbstring" Functions
Managing HTTP Input and Output Encoding
Managing Non-ASCII Character Strings with MySQL Servers