Differences and usage of substr () mb_substr () mb_struct () in PHP and Smar .... Differences and usage of PHPsubstr (), mb_substr () and mb_strcut and differences between the PHPsubstr () function and the truncate regulator in the Smarty Template, however, if the text to be split includes the differences and usage of the Chinese text PHP substr (), mb_substr () and mb_strcut, and the truncate regulator in the Smarty template
The PHP substr () function can split text. However, if the text to be split contains Chinese characters, the function mb_substr ()/mb_strcut can be used () the usage of/mb_strcut is similar to that of substr (), but an additional parameter must be added at the end of mb_substr ()/mb_strcut to set the character string encoding. However, php_mbstring.dll is not enabled on the server, in php. ini opens php_mbstring.dll.
For example:
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
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 ......
Description of the mbstring function:
The mbstring extension module of php provides the processing capability of multi-byte characters. The most common feature is to use mbstring to split multi-byte Chinese characters, which can avoid the occurrence of half a character, because it is an extension of php, its performance is better than some custom multibyte splitting functions.
Mbstring extension provides several function-like functions, mb_substr and mb_strcut. let's take a look at their explanations in the manual.
Mb_substr
Mb_substr () returns the portion of str specified by the start and length parameters.
Mb_substr () performs multi-byte safe substr () operation based on number of characters. position is counted from the beginning of str. first character's position is 0. second character position is 1, and so on.
Mb_strcut
Mb_strcut () returns the portion of str specified by the start and length parameters.
Mb_strcut () performs equivalent operation as mb_substr () with different method. If start position is multi-byte character's second byte or larger, it starts from first byte of multi-byte character.
It subtracts string from str that is shorter than length AND character that is not part of multi-byte string or not being middle of shift sequence.
For another example, we use mb_substr and mb_strcut to split a piece of text:
PLAIN TEXT
CODE:
$ Str = 'I am a long string of Chinese characters -www.webjx.com ';
Echo "mb_substr:". mb_substr ($ str, 0, 6, 'utf-8 ');
Echo"
";
Echo "mb_strcut:". mb_strcut ($ str, 0, 6, 'utf-8 ');
?>
The output result is as follows:
Mb_substr
Mb_strcut: I am a // The number of bytes. one Chinese character is 3 bytes.
Note: The truncate regulator may be used to intercept strings when using the smarty template. at this time, garbled characters will occur when the intercepted strings contain Chinese characters (in the case of variable-length encoding, generally, the length of a string is measured by the number of characters rather than the number of bytes. in this case, we can define a variable regulator to intercept a string based on the number of characters. Method: 1. use the ord () function in php to obtain the character ascii code. 2. use the substr () function to intercept the character string based on the ascii code, so that there is no garbled text.
Differences and usage of http://www.bkjia.com/PHPjc/477951.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477951.htmlTechArticlePHP substr (), mb_substr () and mb_strcut and the introduction of the truncate regulator in the Smarty template to the PHP substr () function can separate text, however, if the text to be split includes Chinese text...