Procedure 1: PHP method of intercepting Chinese strings.
Since garbled characters (using substr) often appear when intercepting Chinese strings on the homepage of the website and vTigerCRM, today I found a better method of intercepting Chinese strings and would like to share it with you.
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 string 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 string will be returned
* @param $str source string
* $len is the length of the substring on the left
*************************************************** ******************/
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
<?
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<count($info[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<strlen($string);$i++)
{
echo cutstr($string,$i)."<br>";
}
?>
Intercepting UTF-8 string function
In order to support multiple languages, the string in the database may be saved as UTF-8 encoding. In 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 consist of 1~3 bytes. The specific number can be determined by the first judged by bytes. (Theoretically it could be longer, but here we assume no more than 3 bytes)
If the first byte is greater than 224, it and the 2 bytes after it form a UTF-8 character. If the first byte is greater than 192 and less than 224, it and the 1 byte after it form a UTF-8 character. Character Otherwise the first byte itself is an English character (including numbers and a small amount of punctuation).
Code previously designed for a website (also the length interception function currently used on the homepage)
Code:
<?php // Cut_Str;
//$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);//The number of bytes in the 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 high and 224,
{
$returnstr=$returnstr.substr($sourcestr,$i,3); //According to 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 counts as 1
}
elseif ($ascnum>=192) //If the ASCII bit is high and 192,
{
$returnstr=$returnstr.substr($sourcestr,$i,2); //According to 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 counts as 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 appearance, uppercase letters are counted as one high-bit character
}
else //In other cases, include 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;
}
Intercept utf-8 string function
function FSubstr($title,$start,$len="",$magic=true)
{
/**
* powered by Smartpig
* mailto:[email protected]
*/
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<strlen($title);$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);
}