Ask, how to determine whether it is low-byte?
Header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK");
$str = "J This is CES test dkfjdksj";
function SUBSTR_GBK ($str, $start, $end)
{
$temp = "";
for ($i = $start; $i <= $end; $i + +)
{
if (Ord ($str {$i}) >0xa0)
{
$temp. =substr ($str, $i, 2);
$i + +;
}else
{
$temp. =substr ($str, $i, 1);
}
}
return $temp;
}
Echo SUBSTR_GBK ($str, 1,10); Intercept normal from high byte
Echo SUBSTR_GBK ($str, 2,10); Garbled truncation from low byte
?>
The above code is truncated from high byte without problem, but if truncation from low byte is garbled
Do you know how to determine the high and low byte method?
Share to:
------Solution--------------------
This post was last edited by xuzuning on 2013-10-30 11:08:53
$str = "J This is CES test dkfjdksj";
function SUBSTR_GBK ($str, $start, $end)
{
$temp = "";
for ($i =0; $i < $start; $i + +) if (Ord ($str {$i}) >0xa0) $i + +; Plus this line.
for (; $i <= $end; $i + +)
{
if (Ord ($str {$i}) >0xa0)
{
$temp. =substr ($str, $i, 2);
$i + +;
}else
{
$temp. =substr ($str, $i, 1);
}
}
return $temp;
}
Echo SUBSTR_GBK ($str, 1,10); This is the CES test.
Echo SUBSTR_GBK ($str, 2,10); It's a CES test.