In php, The substr (), mb_substr (), and mb_strcut functions are character truncation functions. However, when substr intercepts Chinese characters, garbled characters are generated, and the two support Chinese truncation. I will introduce them below.
Substr () function
Substr (string, start, length)
String indicates the object to be intercepted, start indicates the starting position, 0 indicates the starting point, positive number indicates the position after the number, and negative number indicates the position starting from the end, but it is still left-to-right cut, length indicates the cut length. negative number indicates how many characters are excluded or ignored. for example:
The Code is as follows: |
Copy code |
<? Php $ Siteurl = 'www. bKjia. c0m '; Print_r (substr ($ siteurl, 4); exit;
|
// Returns bKjia. c0m, which starts with 4th characters and returns all subsequent characters.
The Code is as follows: |
Copy code |
<? Php $ Siteurl = 'www. bKjia. c0m '; Print_r (substr ($ siteurl,-6, 2); exit; |
To intercept two-byte Chinese characters. use the PHP mb_substr function or the mb_strcut function, but these two functions depend on the php extension php_mbstring.dll component, so configure your server. copy the php_mbstring.dll file in the php installation directory to the windows/system32 directory of drive C of your Windows 2003.
For example:
The Code is as follows: |
Copy code |
<? Php Echo mb_substr ('in this way, my strings will not contain garbled characters such as ^_^', 0, 7, 'utf-8 '); ?> |
Output: in this way, my words
The Code is as follows: |
Copy code |
<? Php Echo mb_strcut ('in this way, my strings will not contain garbled characters such as ^_^', 0, 7, 'utf-8 '); ?> |
Output:
From the above example, we can see that mb_substr is a word-based splitting character, while mb_strcut is a byte-based splitting character, but it does not produce a half character ......
The Code is as follows: |
Copy code |
<? Php Echo mb_substr ('flying garden blog feihuayuan ); // Return: Apsara infrastructure management framework Echo mb_substr ('flying garden blog feihuayuan ', 'utf-8 '); // Return: feih Mb_strcut ('flying garden blog feihuayuan ', 'utf-8 '); Then return: Apsara infrastructure management framework ?> |
For another example, we use mb_substr and mb_strcut to split a piece of text:
PLAIN TEXT
CODE:
The Code is as follows: |
Copy code |
<? Php $ Str = 'I am a long string of Chinese characters -'; Echo "mb_substr:". mb_substr ($ str, 0, 6, 'utf-8 '); Echo "<br> "; Echo "mb_strcut:". mb_strcut ($ str, 0, 6, 'utf-8 '); ?> |
The output result is as follows:
Mb_substr: I am a comparison string
Mb_strcut: I am
Summary
From the above example, we can see that substr only supports single-byte, which is only suitable for English intercept, while the mb_substr function is a dual-font intercept, which can be used in Chinese, while mb_strcut is three bytes.