Procedure 1: PHP method of intercepting Chinese strings
Since garbled characters (using substr) often appear when intercepting Chinese strings on the website homepage and vTigerCRM, today I found a better method of intercepting Chinese strings. Share it with everyone here.
Copy to ClipboardQuoted content: [www.bkjia.com]
function msubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen ; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$ i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
Program 2: PHP intercepts UTF-8 strings and solves the half-character problem
Copy to ClipboardQuoted content:
[www.bkjia.com]
/***************************************************** ****************
* 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
**************************** *****************************************/
function utf_substr($str,$len)
{
for($i=0;$i<$len;$i++)
{
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127)
{
$i++;
if($i<$len)
{
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}
else
{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}
?>
php utf-8 string interception
Copy to ClipboardQuoted content:
[www.bkjia.com]
function cutstr($string, $length) {
preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf ]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf] [x80-xbf]/", $string, $info);
for($i=0; $i
$wordscut .= $info [0][$i];
$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $ length - 3) {
return $wordscut." ...";
}
}
return join('', $info[0]);
}
$ string="242432 Opposition is 456 committed on the broad embassy local 7890";
for($i=0;$i
{
echo cutstr($string ,$i)."
";
}
?>
Interception UTF-8 string function
In order to support multiple languages, the strings in the database may be saved as UTF-8 encoding. During website development, you may need to use PHP to intercept part of the string. In order to avoid garbled characters, write the following UTF-8 string interception function
For the principle of utf-8, please see UTF-8 FAQ
UTF-8 encoded characters may be from 1~ It consists of 3 bytes. The specific number can be judged from the first byte. (Theoretically it may be longer, but here it is assumed that it does not exceed 3 bytes)
The first byte is greater than 224, it and the 2 bytes after it form a UTF-8 character
The first If a byte is greater than 192 and less than 224, it and the 1 byte after it form a UTF-8 character
. Otherwise, the first byte itself is an English character (including numbers and a small part of punctuation marks).
Code previously designed for a website (also the length interception function currently used on the homepage)
Copy to ClipboardQuoted content:
[www.bkjia.com]
//$sourcestr is the string to be processed
//$cutlength is the length of the interception (i.e. the number of words)
function cut_str($sourcestr,$cutlength)
{
$returnstr='';
$i=0;
$n=0;
$str_length=strlen($sourcestr);//Number of bytes of string
while (($n<$cutlength) and ($i<=$str_length))
{
$temp_str=substr($sourcestr,$i,1);
$ascnum=Ord($temp_str );//Get the ASCII code of the $i-th character in the string
if ($ascnum>=224) //If the ASCII bit is higher than 224,
{
$returnstr=$returnstr.substr ($sourcestr,$i,3); //According to the UTF-8 encoding specification, 3 consecutive characters are counted as a single character
$i=$i+3; //The actual Byte is counted as 3
$n++; //String length is counted as 1
}
elseif ($ascnum>=192) //If the ASCII bit is higher than 192,
{
$returnstr=$returnstr.substr($ sourcestr,$i,2); //According to the UTF-8 encoding specification, 2 consecutive characters are counted as a single character
$i=$i+2; //The actual Byte is counted as 2
$n++ ; //String length is 1
}
elseif ($ascnum>=65 && $ascnum<=90) //If it is an uppercase letter,
{
$returnstr=$returnstr.substr ($sourcestr,$i,1);
$i=$i+1; //The actual number of Bytes still counts as 1
$n++; //But considering the overall aesthetics, capital letters count as a high digit Characters
}
else //In other cases, including lowercase letters and half-width punctuation marks,
{
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1; //The actual Byte count is 1
$n=$n+0.5; //Lowercase letters and half-width punctuation marks are half the width of high-order characters...
}
}
if ($str_length>$cutlength){
$returnstr = $returnstr . "...";//Add an ellipsis at the end when the length exceeds
}
return $ returnstr;
}
Interception utf-8 string function
Copy to Clipboard引用的内容:
[www.bkjia.com]
function FSubstr($title,$start,$len="",$magic=true)
{
if($len == "") $len=strlen($title);
if($start != 0)
{
$startv = ord(substr($title,$start,1));
if($startv >= 128)
{
if($startv < 192)
{
for($i=$start-1;$i>0;$i--)
{
$tempv = ord(substr($title,$i,1));
if($tempv >= 192) break;
}
$start = $i;
}
}
}
if(strlen($title)<=$len) return substr($title,$start,$len);
$alen = 0;
$blen = 0;
$realnum = 0;
for($i=$start;$i
{
$ctype = 0;
$cstep = 0;
$cur = substr($title,$i,1);
if($cur == "&")
{
if(substr($title,$i,4) == "<")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,4) == ">")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,5) == "&")
{
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == """)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(preg_match("/(\d+);?/i",substr($title,$i,8),$match))
{
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0])-1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}
}else{
if(ord($cur)>=252)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}elseif(ord($cur)>=248){
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$ctype = 1;
$blen ++;
}
}elseif(ord($cur)>=240){
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}elseif(ord($cur)>=224){
$cstep = 3;
$length += 3;
$i += 2;
$realnum ++;
if($magic)
{
$ctype = 1;
$blen ++;
}
}elseif(ord($cur)>=192){
$cstep = 2;
$length += 2;
$i += 1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}elseif(ord($cur)>=128){
$length += 1;
}else{
$cstep = 1;
$length +=1;
$realnum ++;
if($magic)
{
if(ord($cur) >= 65 && ord($cur) <= 90)
{
$blen++;
}else{
$alen++;
}
}
}
}
if($magic)
{
if(($blen*2+$alen) == ($len*2)) break;
if(($blen*2+$alen) == ($len*2+1))
{
if($ctype == 1)
{
$length -= $cstep;
break;
}else{
break;
}
}
}else{
if($realnum == $len) break;
}
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);
return substr($title,$start,$length);
}
http://www.bkjia.com/PHPjc/364371.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364371.htmlTechArticle程序一:PHP截取中文字符串方法 由于网站首页以及vTigerCRM里经常在截取中文字符串时出现乱码(使用substr),今天找到一个比较好的截取中文...