php中文汉字字符串的截取问号
php在操作字符串的问题时间无非两个问题:
1.判断字符串编码是gbk还是unicode。
2.对相应编码采取相应截取方法。
一般情况下我们使用substr截取汉字可能会遇到乱码问题。因为汉字是双字节的,当被截取了一个字节时,这个汉字就无法显示,乱掉了。
其实解决很简单,看下面的截取函数:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6189')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6189>//截取超长字符串
function curtStr($str,$len=30){
if(strlen($str)>$len){
$str = substr($str,0,$len);
$str .= chr(0) ."…";
return $str;
}
上面的chr(0)不是null
null是什么都没有,而chr(0)的值是0。表示成16进制是0×00,表示成二进制是00000000
虽然chr(0)不会显示出什么,但是他是一个字符。
当汉字被截断时,根据编码规则他总是要把后边的其他字符拉过来一起作为汉字解释,这就是出现乱码的原因。而值为0×81到0xff与0×00组合始终都显示为“空”
根据这一特点,在substr的结果后面补上一个chr(0),就可以防止出现乱码了
下面补充几个函数即可实现此两点以达到精确截取中文字符串的目的:
截取utf8编码的多字节字符串
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9246')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9246> <?php
//截取utf8字符串
function utf8Substr($str, $from, $len)
{
return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF] ){0,'.$from.'}'.
'((?:[x00-x7F]|[xC0-xFF][x80-xBF] ){0,'.$len.'}).*#s',
'$1',$str);
}
?>
UTF-8、GB2312都支持的汉字截取函数
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6264')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6264> <?php/*
Utf-8、gb2312都支持的汉字截取函数
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 utf-8
开始长度默认为 0
*/
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
if($code == 'UTF-8')
{
$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i )
{
if($i>=$start && $i<($start $sublen))
{
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i ;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
$str = "abcd需要截取的字符串";
echo cut_str($str, 8, 0, 'gb2312');
?>
您可能感兴趣的文章:
php中文字符截取函数(自用)
php截取字符串(无乱码 utf8)
php截取中文字符串乱码如何解决呢
php中文字符串截取方法
php截取字符串实例代码
php如何截取字符串后四位
php分割GBK中文乱码的解决方法
php读取html并截取字符串的小例子
截取中文字符的函数-csubstr
php中文截取字符串mb_substr的用法