We are doingPHP function Mb_strcut, their two usage is similar to substr (), but in the PHP function mb_strcut the last to add a parameter to set the string encoding, but the general server is not open php_ Mbstring.dll, need to php.ini in the Php_mbstring.dll to open, if we are not sure whether to open this function, then we'd better first to judge, if (Function_exist (mb_string)).
As an example:
- <? PHP
- echo mb_substr (' So my string
There will be no garbled ^_^ ', 0, 7, ' utf-8 ');
- ?>
Output: so that my word
Example of PHP function mb_strcut
- <? PHP
- Echo mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
- ?>
Output: Such a
As can be seen from the above example, MB_SUBSTR is the word to divide the characters, and PHP function mb_strcut is to divide the characters by Byte, but will not produce a half character phenomenon, but sometimes in the output will also appear when the interception of something is incorrect, some right some wrong, And the Chinese also garbled, the main reason for these phenomena is that the way you make this function is inconsistent with the way you display the encoding of his web page.
A custom function that PHP intercepts Chinese strings is also described here:
A method of text string interception without garbled characters in PHP implementation
- function Gbsubstr ($string, $start, $length) {
- if (strlen ($string)>$length) {
- $ Str = NULL ;
- $ Len = $start + $length;
- For ($i= $start; $i<$len; $i + +) {
- if (Ord (substr ($string, $i, 1))>0xa0) {
- $ str. = substr ($string, $i, 2);
- $i + +;
- }else{
- $ str. = substr ($string, $i, 1);
- }
- }
- return $str. ' ... ';
- }else{
- return $string;
- }
- }
The above is garbled after the use of PHP function mb_strcut The main steps to solve the problem.
http://www.bkjia.com/PHPjc/446298.html www.bkjia.com true http://www.bkjia.com/PHPjc/446298.html techarticle we are in the PHP function Mb_strcut, their usage is similar to substr (), but in the PHP function mb_strcut the last to add a parameter to set the string encoding, but the general ...