/*
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;
}
?>