/*
PHP uses iso-8859-1 to process strings, that is, to process strings in a single byte. The Chinese code is a multibyte character, in the use of substr,
Strlen,str_peplace such functions are prone to errors, handling Chinese special operations, you must use a set of Chinese processing functions. Below is
GBK Substr,strlen operation function, the former is not like PHP substr can use negative numbers as start,lenght. I hope you
To continuously improve these functions to achieve a high degree of 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;
}
?>
http://www.bkjia.com/PHPjc/445582.html www.bkjia.com true http://www.bkjia.com/PHPjc/445582.html techarticle / * PHP uses ISO-8859-1 to process strings, that is, to process strings in a single byte. Chinese code is multi-byte characters, in the use of substr, strlen,str_peplace and other functions are prone to error, where ...