This article summarizes the function to intercept strings based on the PHP function substr. However, when encountering Chinese, half of the Chinese characters are intercepted and garbled characters appear. The following introduces the interception program that supports Chinese and other encodings.
The code is as follows | Copy code | ||||||||
* String interception, supports Chinese and other encodings * * @static * @param string $suffix truncation of display characters elseif(function_exists('iconv_substr') ) { Iconv_substr($str,$start,$length,$charset);} $re['utf-8'] = "/[x01-x7f]|[xc2-xdf] [x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
|
The code is as follows | Copy code |
function msubstr($str, $start, $len) { $tmpstr = ""; $strlen = $start + $len; for($ i = 0; $i < $strlen; $i++) {<🎜> $i, 2);<🎜> $i++;<🎜> } else<🎜> $tmpstr .= substr($str, $i, 1);<🎜> }<🎜> return $tmpstr;<🎜>} <🎜> |
Program 2: PHP intercepts UTF-8 strings and solves the half-character problem
/***************************************************** ******************
* PHP intercepts UTF-8 strings to solve the half-character problem.
* English and numbers (half-width) are 1 byte (8 bits), Chinese (full-width) are 3 bytes
* @return The string taken out, when $len is less than or equal to 0, the entire character will be returned String
* @param $str Source string
* $len The length of the left substring
**************************** *****************************************/
The code is as follows
| Copy code | ||||
function utf_substr($str,$len){ for($i=0;$i<$len;$i++){ $temp_str=substr($str,0,1); }}return join($new_str);} ?>
http://www.bkjia.com/PHPjc/444686.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444686.htmlTechArticleThis article summarizes the function to intercept strings based on the php function substr, but it appears when encountering Chinese The solution to the problem of garbled characters when half of Chinese characters are intercepted is introduced below...
|