PHP Chinese processing function

Source: Internet
Author: User
Tags functions ord strlen
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;
}

?>







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.