首页 >后端开发 >php教程 >PHP实现截取字符串的通用方法

PHP实现截取字符串的通用方法

WBOY
WBOY原创
2016-07-25 08:45:30988浏览
  1. /**
  2. * 截取字符串
  3. * params $string 要截取的字符串
  4. * params $length: 保留长度(字符数)
  5. * params $dot: 多余部分显示
  6. **/
  7. function _cutstr($string, $length, $dot = ' ...') {
  8. if(strlen($string) return $string;
  9. }
  10. $string = str_replace(array('&', '"', ''), array('&', '"', ''), $string);
  11. $strcut = '';
  12. $n = $tn = $noc = 0;
  13. while($n $t = ord($string[$n]);
  14. if($t == 9 || $t == 10 || (32 $tn = 1; $n++; $noc++;
  15. } elseif(194 $tn = 2; $n += 2; $noc += 2;
  16. } elseif(224 $tn = 3; $n += 3; $noc += 2;
  17. } elseif(240 $tn = 4; $n += 4; $noc += 2;
  18. } elseif(248 $tn = 5; $n += 5; $noc += 2;
  19. } elseif($t == 252 || $t == 253) {
  20. $tn = 6; $n += 6; $noc += 2;
  21. } else {
  22. $n++;
  23. }
  24. if($noc >= $length) {
  25. break;
  26. }
  27. }
  28. if($noc > $length) {
  29. $n -= $tn;
  30. }
  31. $strcut = substr($string, 0, $n);
  32. $strcut = str_replace(array('&', '"', ''), array('&', '"', ''), $strcut);
  33. return $strcut.$dot;
  34. }
复制代码

PHP


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn