PHP切割汉字的常用方法实例总结


Posted in PHP onApril 27, 2019

本文实例讲述了PHP切割汉字的常用方法。分享给大家供大家参考,具体如下:

<?php
/*
@UTF-8编码的字符可能由1~3个字节组成。
*/
/*--------------------------方法一截取中文字符串方法------------------------------*/
function msubstr($str, $start, $len)
{
  $tmpstr = "";
  $strlen = $start + $len;
  for ($i = 0; $i < $strlen; $i++) {
    if (ord(substr($str, $i, 1)) > 0xa0)  //ord()函数返回字符串的第一个字符的ASCII值
    {
      $tmpstr .= substr($str, $i, 2);
      $i++;
    } else {
      $tmpstr .= substr($str, $i, 1);
    }
  }
  return $tmpstr;
}
/*----------------------------第二种方法-----------------------------------*/
//截取的是UTF-8字符串
function utf_substr($str, $len)
{
  $new_str = [];
  for ($i = 0; $i < $len; $i++) {
    $tem_str = substr($str, 0, 1);
    if (ord($tem_str > 127)) {
      $i++;
      if ($i < $len) {
        $new_str[] = substr($str, 0, 3);
        $str = substr($str, 3);
      }
    } else {
      $new_str[] = substr($str, 0, 1);
      $str = substr($str, 1);
    }
  }
  return join($new_str);//join()函数把数组元素组合为一个字符串
}
/*-------------------------------------第三种方法(UTF-8)--------------------------------*/
function cutstr($string, $length)
{
  preg_match_all("/[\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]/", $string, $info);
  $wordscut = "";
  $j = 0;
  for ($i = 0; $i < count($info[0]); $i++) {
    $wordscut .= $info[0][$i];
    $j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
    if ($j > $length - 3) {
      return $wordscut . "...";
    }
  }
  return join('', $info[0]);
}
$string = "312哈哈,这个组合很难切割哦";
echo cutstr($string, 10);
/*---------------------------------下面是曾经用过的截取第三个的字符串的------------------------------*/
// $name1 = mysql_result($my_rst,0,"name");
// $name = preg_match("/([1-9][0-9]+)/",$name1,$r);
// $name = $r[0];
// if($name == ""){
// $name=preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,2}'.
// '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,1}).*#s',
// '$1',$name1);
// }
/*--------------------------------------------第四种方法(UTF-8)---------------------------------------------*/
function cut_str($sourcestr, $cutlength)
{
  $returnstr = '';
  $i = 0;
  $n = 0;
  $str_length = strlen($sourcestr);//字符串的字节数
  while ($n < $cutlength && $i <= $str_length) {
    $temp_str = substr($sourcestr, $i, 1);
    $ascnum = ord($temp_str);//得到字符串中第$i位字符的ASCII码
    if ($ascnum >= 224) {
      $returnstr = $returnstr . substr($sourcestr, $i, 3);//根据UTF-8编码规范,将3个连续的字符计为单个字符
      $i = $i + 3;//实际Byte记为3
      $n++;//字串长度为1
    } elseif ($ascnum >= 192)//如果ASCII位高于192
    {
      $returnstr = $returnstr . substr($sourcestr, $i, 2);//根据UTF-8编码规范,将2个连续的字符记为单个字符
      $i = $i + 2;//实际Byte记为2
      $n++;//字串长度为1
    } elseif ($ascnum >= 65 && $ascnum <= 90)//如果是大写字母
    {
      $returnstr = $returnstr . substr($sourcestr, $i, 1);
      $i = $i + 1;//byte记为1
      $n++;//但考虑到整体美观,大写字母计成一个高位字符
    } else {
      $returnstr = $returnstr . substr($sourcestr, $i, 1);
      $i = $i + 1;//实际的Byte记为1
      $n = $n + 0.5;//小写字母和半角标点等与半个高位字符宽...
    }
  }
  if ($str_length > $cutlength) {
    $returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
  }
  return $returnstr;
}
/*--------------------第五种方法(UTF-8)---------------------------------------------*/
function FSubstr($title, $start, $len = "", $magic = true)
{
  if ($len == "") $len = strlen($title);
  if ($start != 0) {
    $startv = ord(substr($title, $start, 1));
    if ($startv >= 128) {
      if ($startv < 192) {
        for ($i = $start - 1; $i > 0; $i--) {
          $tempv = ord(substr($title, $i, 1));
          if ($tempv >= 192) break;
        }
        $start = $i;
      }
    }
  }
  if (strlen($title) <= $len) return substr($title, $start, $len);
  $alen = 0;
  $blen = 0;
  $realnum = 0;
  $length = 0;
  for ($i = $start; $i < strlen($title); $i++) {
    $ctype = 0;
    $cstep = 0;
    $cur = substr($title, $i, 1);
    if ($cur == "&") {
      if (substr($title, $i, 4) == "<") {
        $cstep = 4;
        $length += 4;
        $i += 3;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (substr($title, $i, 4) == ">") {
        $cstep = 4;
        $length += 4;
        $i += 3;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (substr($title, $i, 5) == "&") {
        $cstep = 5;
        $length += 5;
        $i += 4;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (substr($title, $i, 6) == """) {
        $cstep = 6;
        $length += 6;
        $i += 5;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (preg_match("/&#(\d+);?/i", substr($title, $i), $match)) {
        $cstep = strlen($match[0]);
        $length += strlen($match[0]);
        $i += strlen($match[0]) - 1;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      }
    } else {
      if (ord($cur) >= 252) {
        $cstep = 6;
        $length += 6;
        $i += 5;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      } elseif (ord($cur) >= 248) {
        $cstep = 5;
        $length += 5;
        $i += 4;
        $realnum++;
        if ($magic) {
          $ctype = 1;
          $blen++;
        }
      } elseif (ord($cur) >= 240) {
        $cstep = 4;
        $length += 4;
        $i += 3;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      } elseif (ord($cur) >= 224) {
        $cstep = 3;
        $length += 3;
        $i += 2;
        $realnum++;
        if ($magic) {
          $ctype = 1;
          $blen++;
        }
      } elseif (ord($cur) >= 192) {
        $ctype = 2;
        $length += 2;
        $i += 1;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      } elseif (ord($cur) >= 128) {
        $length += 1;
      } else {
        $cstep = 1;
        $length += 1;
        $realnum++;
        if ($magic) {
          if (ord($cur) >= 65 && ord($cur) <= 90) {
            $blen++;
          } else {
            $alen++;
          }
        }
      }
    }
    if ($magic) {
      if (($blen * 2 + $alen) == ($len * 2)) break;
      if (($blen * 2 + $alen) == ($len * 2) + 1) {
        if ($ctype == 1) {
          $length -= $cstep;
          break;
        } else {
          break;
        }
      }
    } else {
      if ($realnum == $len) break;
    }
  }
  unset($cur);
  unset($alen);
  unset($blen);
  unset($realnum);
  unset($ctype);
  unset($cstep);
  return substr($title, $start, $length);
}
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);
}
$title = "你哈珀niad1纳斯达wop asdni你爱谁都没阿斯顿撒旦12ccs- sd";
$title = utf8Substr($title, 0, 15);
echo $title;
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
php 什么是PEAR?
Mar 19 PHP
php array_push()数组函数:将一个或多个单元压入数组的末尾(入栈)
Jul 12 PHP
php 记录进行累加并显示总时长为秒的结果
Nov 04 PHP
php安全配置 如何配置使其更安全
Dec 16 PHP
深入理解:XML与对象的序列化与反序列化
Jun 08 PHP
解析PHP中的file_get_contents获取远程页面乱码的问题
Jun 25 PHP
将word转化为swf 如同百度文库般阅读实现思路及代码
Aug 09 PHP
eaglephp使用微信api接口开发微信框架
Jan 09 PHP
PHP面向对象之后期静态绑定功能介绍
May 18 PHP
Yii数据模型中rules类验证器用法分析
Jul 15 PHP
php利用array_search与array_column实现二维数组查找
Jul 08 PHP
thinkPHP3.2使用RBAC实现权限管理的实现
Aug 27 PHP
YII框架常用技巧总结
Apr 27 #PHP
YII框架行为behaviors用法示例
Apr 26 #PHP
YII框架模块化处理操作示例
Apr 26 #PHP
Yii框架ACF(accessController)简单权限控制操作示例
Apr 26 #PHP
YII框架实现自定义第三方扩展操作示例
Apr 26 #PHP
PHP中PCRE正则解析代码详解
Apr 26 #PHP
通过PHP设置BugFree获取邮箱通知
Apr 25 #PHP
You might like
PHP文章采集URL补全函数(FormatUrl)
2012/08/02 PHP
PHP中$_FILES的使用方法及注意事项说明
2014/02/14 PHP
php使用反射插入对象示例分享
2014/03/11 PHP
PHP7使用ODBC连接SQL Server2008 R2数据库示例【基于thinkPHP5.1框架】
2019/05/06 PHP
javascript异步编程的4种方法
2014/02/19 Javascript
微信小程序  audio音频播放详解及实例
2016/11/02 Javascript
js实现复选框的全选和取消全选效果
2017/01/03 Javascript
Javascript实现一个简单的输入关键字添加标签效果实例
2017/06/01 Javascript
js学习总结之DOM2兼容处理重复问题的解决方法
2017/07/27 Javascript
三分钟学会用ES7中的Async/Await进行异步编程
2018/06/14 Javascript
详解Angular6学习笔记之主从组件
2018/09/05 Javascript
jQuery实现数字自动增加或者减少的动画效果示例
2018/12/11 jQuery
vue项目创建并引入饿了么elementUI组件的步骤
2019/04/11 Javascript
微信小程序 select 下拉框组件功能
2019/09/09 Javascript
jQuery实现轮播图源码
2019/10/23 jQuery
关于引入vue.js 文件的知识点总结
2020/01/28 Javascript
详细分析vue表单数据的绑定
2020/07/20 Javascript
使用pandas读取csv文件的指定列方法
2018/04/21 Python
python 每天如何定时启动爬虫任务(实现方法分享)
2018/05/21 Python
pandas Dataframe行列读取的实例
2018/06/08 Python
如何利用Python分析出微信朋友男女统计图
2019/01/25 Python
对pandas处理json数据的方法详解
2019/02/08 Python
Django的models中on_delete参数详解
2019/07/16 Python
Python 经典算法100及解析(小结)
2019/09/13 Python
基于Python执行dos命令并获取输出的结果
2019/12/30 Python
Python 2种方法求某个范围内的所有素数(质数)
2020/01/31 Python
Python实现对adb命令封装
2020/03/06 Python
python 两种方法删除空文件夹
2020/09/29 Python
墨西哥网上超市:Superama
2018/07/10 全球购物
北京泡泡网网络有限公司.net面试题
2012/07/17 面试题
高中军训感言1000字
2014/03/01 职场文书
护校行动方案
2014/05/31 职场文书
社团活动总结怎么写
2014/06/30 职场文书
会计岗位说明书
2014/07/29 职场文书
暂住证明怎么写
2015/06/19 职场文书
sql server偶发出现死锁的解决方法
2022/04/10 SQL Server