Home > php教程 > php手册 > php中文处理函数

php中文处理函数

WBOY
Release: 2016-06-13 11:16:14
Original
1291 people have browsed it

  /*
php使用ISO-8859-1处理字符串,也就是以单字节处理字符串。中文码属于多字节字符,在使用substr,
strlen,str_peplace等函数时很容易产生错误,处理中文特殊操作,必须使用一组中文处理函数。下面是
GBK的substr,strlen操作函数,前者并不像php的substr一样可以使用负数作为start,lenght.希望大家
来不断完善这些函数,达到高效实用的程度。
*/
//gbk str handle
function gbk_substr(&$str,$start,$length=-1)
{
if($length==0) return "";
if($start for($i=0;$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 {
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 {
if(ord(substr($str,$i,1))>=0x81) $i++;
$l++;
}
return $l;
}
function gb2312_strlen(&$str)
{
$len=strlen($str);
$l=0;
for($i=0;$i {
if(ord(substr($str,$i,1))>=0xa1) $i++;
$l++;
}
return $l;
}
?>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template