PHP Chinese processing function _php Tutorial

Source: Internet
Author: User
/*
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)) &GT;=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 ...

  • 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.