教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php截取字符串长度函数详解

php截取字符串长度函数详解

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供php截取字符串长度函数详解等资源,欢迎您收藏本站,我们将为您提供最新的php截取字符串长度函数详解资源
在php中提供了大量字符串操作函数,像计算字符串长度或字符串截取函数,但是他们都只能简单的计算英文字符,不能对中文混合字符串进行操作,下面我来给大家介绍截取字符串长度与计算字符串长度的方法总结。

常用的字符处理函数

<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('copy7620')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7620>

strstr(string,string) = strchr(,) //从前面第一次出现某个字符串的地方截取到最后
strrchr(string,string) //从某个字符串从最后出现的位置截取到结尾
strpos(string,string[,int]) //某个字符串第一次出现的位置
strrpos(string,string) //某个字符串最后一次出现的位置
substr(string,int[,int]) //从指定位置开始截取字符串,可以指定截取的长度。
strlen(string) //获取字符串的长度

假设

$str="这是1个字符串";
这个串中包含了一个半角字符,同样执行:

<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('copy5682')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5682>if(strlen($str)>10) $str=substr($str,10);

由于原字符串$str的第10、11个字符构成了汉字“符”;
执行串分割后会将该汉字一分为二,这样被截取的串就会发现乱码现象

那我们可以先来计算字符串长度

<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('copy5853')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5853>

<?PHP
  header('Content-type: text/html; charset=utf-8');
  $str = "在士大夫了sdfsdfcxvzv一截";

  $pa = '/[x{4e00}-x{9fa5}]/siu';
  preg_match_all($pa, $str, $r);
 
  $count = count($r[0]);
  echo "当前的字符串中,共有 $count 个汉字";
 
  if($count>10)
  {
    //如果汉字数量大于10,你的代码
  }

?>

补充

PHP计算字符串长度,包括计算英文、GBK、UTF-8多种字符集下PHP如何计算字符串长度。英文字符串长度
strlen()是PHP自带的计算英文字符串的函数。

GBK字符串长度
中文字符计算为2个字符,英文字符计算为1个,可以统计中文字符串长度的函数。 function abslength($str){

<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('copy3618')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3618>$len=strlen($str);
$i=0;
while($i<$len)
{
       if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."] $/",$str[$i]))
       {
         $i =2;
       }
       else
       {
         $i =1;
       }
}
return $i;
}

UTF8字符串长度
下面定义的strlen_utf8函数可以统计UTF-8字符串的长度,但不同的是,该函数并不考虑字节,这有些类似

Javascript 中字符串的length方法,一个字符全部按 1 个长度计算。 <?php // 说明:计算 UTF-8 字符串长度(忽

略字节的方案)

<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('copy4842')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4842>function strlen_utf8($str) {
$i = 0;
$count = 0;
$len = strlen ($str);
while ($i < $len) {
$chr = ord ($str[$i]);
$count ;
$i ;
if($i >= $len) break;
if($chr & 0x80) {
$chr <<= 1;
while ($chr & 0x80) {
$i ;
$chr <<= 1;
}
}
}
return $count;
}
$str = "www.jiaochengji.com-PHP资讯";
echo strlen_utf8($str);
?>

这样就可以很准确的对你的中英文混合字体进行截取计算了,如例子

<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('copy2213')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2213>

支持gb2312,gbk,utf-8,big5 中文截取方法

/*  
 
* 中文截取,支持gb2312,gbk,utf-8,big5  
 
*  
 
* @param string $str 要截取的字串  
 
* @param int $start 截取起始位置  
 
* @param int $length 截取长度  
 
* @param string $charset utf-8|gb2312|gbk|big5 编码  
 
* @param $suffix 是否加尾缀  
 
*/
 
public function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)  
 
{  
 
   if(function_exists("mb_substr"))  
 
   {  
 
       if(mb_strlen($str, $charset) <= $length) return $str;  
 
       $slice = mb_substr($str, $start, $length, $charset);  
 
   }  
 
   else
 
   {  
 
       $re['utf-8']   = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff]

[x80-xbf]{3}/";  
 
       $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";  
 
       $re['gbk']          = "/[x01-x7f]|[x81-xfe][x40-xfe]/";  
 
       $re['big5']          = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";  
 
       preg_match_all($re[$charset], $str, $match);  
 
       if(count($match[0]) <= $length) return $str;  
 
       $slice = join("",array_slice($match[0], $start, $length));  
 
   }  
 
   if($suffix) return $slice."…";  
 
   return $slice;  
 
}

您可能感兴趣的文章:
php中文截取字符串函数(很好用)
php如何截取字符串后四位
php截取字符串实例代码
php截取中文字符串乱码如何解决呢
mysql字符串截取函数SUBSTRING的用法
php截取字符串长度函数详解
php读取html并截取字符串的小例子
截取中文字符的函数-csubstr
php截取字符串(无乱码 utf8)
php中英文混排字符串截取方法

[关闭]
~ ~