Home >Web Front-end >JS Tutorial >Jquery implementation code for intercepting Chinese strings_jquery

Jquery implementation code for intercepting Chinese strings_jquery

WBOY
WBOYOriginal
2016-05-16 18:13:451428browse
Copy code The code is as follows:

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 string The ascii code of the $i-th character in
if ($ascnum>=224) //If the ASCII bit is higher than 224,
{
//According to the UTF-8 encoding specification, 3 consecutive Characters are counted as a single character
$returnstr=$returnstr.substr($sourcestr,$i,3);
$i=$i 3; //The actual Byte is counted as 3
$n; // String length is 1
}
elseif ($ascnum>=192) //If the ASCII bit height is equal to 192,
{
//According to the UTF-8 encoding specification, combine 2 consecutive Characters are counted as a single character
$returnstr=$returnstr.substr($sourcestr,$i,2);
$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 Byte number is still counted as 1
$n; //But considering the overall appearance, capital letters are counted as one high-bit character
}
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 are half the width of high-bit characters...
}
}
if ($str_length>$cutlength){
$returnstr = $returnstr . "...";//Add an ellipsis at the end when the length exceeds
}
return $returnstr;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn