PHP string interception function (to solve the Chinese garbled! Support UTF-8 and GB2312)

Code / * *********************************************** *********************     function name: CutString     function role: string interception function, two double-byte characters as word processing     Usage: CutString ( "I am the Chinese ", 5);     parameters:     string $ str need of treatment     $ length len is taken (i.e., words)     $ encode UTF-8 text encoding support and GB2312     ************* ************************************************** ***** * / public static function CutString ( $ STR , $ len , $ encode = ' UTF-. 8 ' / * $ encode = 'GB2312' * / )     { IF ( strlen ( $ STR











)<=$len or $len<1)
{
return $str;
}
else
{
for($i=0;$i<=$len;$i++)
{
$temp_str=substr($str,0,1);
if(ord($temp_str)>127)
{
$i++;
if($i<=$len)
{
if($encode=='utf-8')
{
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
else
{
$new_str[]=substr($str,0,2);
$str=substr($str,2);
}
}
}
else
{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str)."";
}
}

Reproduced in: https: //www.cnblogs.com/200831856/archive/2008/11/28/1343527.html

Guess you like

Origin blog.csdn.net/weixin_34185320/article/details/93711137