Home  >  Article  >  Backend Development  >  PHP Chinese processing function_PHP tutorial

PHP Chinese processing function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:55774browse

/*
PHP uses ISO-8859-1 to process strings, which means strings are processed in single bytes. Chinese codes are multi-byte characters. When using substr,
It is easy to cause errors when using strlen, str_peplace and other functions. To handle Chinese special operations, a set of Chinese processing functions must be used. Below is
GBK's substr and strlen operation functions, the former is not like PHP's substr, which can use negative numbers as start and length. I hope everyone
To continuously improve these functions to achieve high efficiency and practicality.
*/
//gbk str handle
function gbk_substr(&$str,$start,$length=-1)
{
if($length==0) return "";
if($start<0) $start=0;
for($i=0;$i<$start;$i++)
{
if(ord(substr($str,$i,1))>=0x81)
{
$start++;
$i++;
}
}
if($start>gbk_strlen($str)) return "";
$ss="";
if($length==-1)
{
$ss=substr($str,$start);
}
else
{
echo "leghth=".$length."";
for($i=$start;$i<$start+$length;$i++)
{
if(ord(substr($str,$i,1))>=0x81)
{
$ss.=substr($str,$i,2);
$length++;
$i++;
}
else
{
$ss.=substr($str,$i,1);
}
}
}
return $ss;
}
function gbk_strlen(&$str)
{
$len=strlen($str);
$l=0;
for($i=0;$i<$len;$i++)
{
if(ord(substr($str,$i,1))>=0x81) $i++;
$l++;
}
return $l;
}
function gb2312_strlen(&$str)
{
$len=strlen($str);
$l=0;
for($i=0;$i<$len;$i++)
{
if(ord(substr($str,$i,1))>=0xa1) $i++;
$l++;
}
return $l;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445582.htmlTechArticle/* PHP uses ISO-8859-1 to process strings, which means strings are processed in single bytes. Chinese codes are multi-byte characters. It is easy to cause errors when using functions such as substr, strlen, str_peplace, etc....
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