If the ASCII code is greater than 0xa0, it is half Chinese; learn about substr (), Mb_substr (), Ord (). The encoding format used now is UTF8,GBK,GBK2312,BIG5. GBK is the upgrade of GBK2312. Now I have encountered in the development of the basic is UTF8,GBK commonly used in forum BBS. Today, I wrote the next UTF8, Test also said the past, after all, from the online copy down is can be used, but if there is time best to write their own, so only to their own hands is their own.
The simplest is to use
Example 1
The code is as follows |
Copy Code |
echo substr ("Hello world!", 6); ?> output: world! Example 2 echo substr ("Hello world!", 6,5); ?> |
There is no problem in English, but Chinese is a problem, we use the MB_SUBSTR function to deal with it.
/**
*—————————————————————————————————————–
*php has a mbstring extension library (I have been asked this question when I have a friend to interview PHP), but *
The general server did not open the Php_mbstring.dll, you need to
*php.ini the Php_mbstring.dll Open, if there is no permission, then only contact your ISP.
* Because the mb_string is more efficient, so let's check if mb_string can be used:
*—————————————————————————————————————–
*/
The code is as follows |
Copy Code |
if (function_exists (' mb_string ')) { Mb_substr ($string, $start, $length, $encoding); Other codes here } else Mysubstr ($string, $start, $length);//Call your own function ?> |
Start by defining your own functions
The code is as follows |
Copy Code |
/** *———————————- * Define your own function first: *———————————- */ function Mysubstr ($string, $start, $length) { if (strlen ($string) > $length) { $str = "; $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;} } |
There is another problem to note that when using this method, the charset of your tag pair can only be encoded in Chinese, such as gbk,gb2312.
The following is a common Chinese character intercept function
Custom intercept Chinese string function, basically is modeled mb_substr write a function, directly call it, the following is the code I found, actually very simple
The code is as follows |
Copy Code |
function Substr_cn ($string _input, $start, $length) { /* Features: * This algorithm is used to intercept Chinese strings * functions are intercepted in a single full character, that is, an English character and a Chinese character represent a unit length Parameters * Parameter $string is the string to intercept, * Parameter $start is the starting position to intercept, * Parameter $length is the number of characters to intercept (one Chinese character or English character is counted) * Return value: * Returns the Intercept result string * */ $str _input= $string _input; $len = $length; $return _str= ""; Defining an empty string for ($i =0; $i <2* $len +2; $i + +) $return _str= $return _str. " "; $start _index=0; Calculates the start byte offset for ($i =0; $i < $start; $i + +) { if (Ord ($str _input{$start _index}>=161))//IS Chinese { $start _index+=2; } else//Is English { $start _index+=1; } } $CHR _index= $start _index; Intercept for ($i =0; $i < $len; $i + +) { $asc =ord ($str _input{$chr _index}); if ($asc >=161) { $return _str{$i}=CHR ($ASC); $return _str{$i +1}=chr (ord ($str _input{$chr _index+1)); $len +=1; End Condition plus 1 $i + +; Position offset plus 1 $CHR _index+=2; Continue } Else { $return _str{$i}=CHR ($ASC); $CHR _index+=1; } } Return trim ($return _str); }//end of SUBSTR_CN ?> |
http://www.bkjia.com/PHPjc/632151.html www.bkjia.com true http://www.bkjia.com/PHPjc/632151.html techarticle If the ASCII code is greater than 0xa0, it is half Chinese; learn about substr (), Mb_substr (), Ord (). The encoding format used now is UTF8,GBK,GBK2312,BIG5. GBK is the upgrade of GBK2312. Now I am in the development of ...