加入收藏 | 设为首页 | 会员中心 | 我要投稿 濮阳站长网 (https://www.0393zz.cn/)- 专属主机、数据湖、操作系统、媒体智能、数据分析!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php中文字符串截取函数

发布时间:2022-06-17 08:43:23 所属栏目:PHP教程 来源:互联网
导读:下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些. ?php //php 中文字符串截取函数 /* */ function substr($str = , $offset = 0, $len = 0){ $len || ($len = strlen
下面这二款函数是二款双字节字符串截取函数,那就是针对中文字符串截取了,好了第一款汉字中文截取函数是越级简洁了,后一款复杂但考虑更多一些.
 
<?php
//php 中文字符串截取函数
/*
 
*/
function substr($str = '', $offset = 0, $len = 0){
    $len || ($len = strlen($str));
    preg_match_all('/./us', $str, $result);
    return implode('', array_slice($result[0], $offset, $len));
}  
 
//方法二,代码如下
if (!function_exists('mb_substr')) {
function mb_substr($str, $start, $len = '', $encoding="utf-8"){
  $limit = strlen($str);
 
  for ($s = 0; $start > 0;--$start) {// found the real start
    if ($s >= $limit)
      break;
 
    if ($str[$s] <= "")
      ++$s;
    else {
      ++$s; // skip length
 
      while ($str[$s] >= "€" && $str[$s] <= "�")
        ++$s;
    }
  }
 
  if ($len == '')
    return substr($str, $s);
  else
    for ($e = $s; $len > 0; --$len) {//found the real end
      if ($e >= $limit)
        break;
 
      if ($str[$e] <= "")
        ++$e;
      else {
        ++$e;//skip length
 
        while ($str[$e] >= "€" && $str[$e] <= "�" && $e < $limit)
          ++$e;//开源代码phpfensi.com
      }
    }
 
  return substr($str, $s, $e - $s);
}
}
 
 
?>
 

(编辑:濮阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读