Functions | Chinese
<?
/*
PHP uses iso-8859-1 to process strings, that is, to handle strings in a single byte. Chinese code belongs to multibyte characters, in the use of substr,
Strlen,str_peplace and other functions can easily produce errors, handling Chinese special operations, you must use a set of Chinese processing functions. The following is the
GBK substr,strlen action function, which does not use negative numbers as a start,lenght, as in PHP substr. I hope everyone
will continue to improve these functions to achieve efficient and practical degree.
*/
//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. " \ n ";
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;
}
?>