首页  >  文章  >  后端开发  >  php 摘要生成函数(自定义,无乱码)

php 摘要生成函数(自定义,无乱码)

WBOY
WBOY原创
2016-07-25 09:04:09942浏览
  1. /**
  2. php摘要生成函数
  3. link:http://bbs.it-home.org 2013-3-7
  4. */
  5. function cutstr($string, $length,$charset,$dot) {//字符,截取长度,字符集,结尾符
  6. if(strlen($string) return $string;
  7. }
  8. $pre = chr(1);
  9. $end = chr(1);
  10. //保护特殊字符串
  11. $string = str_replace(array('&', '"', ''), array($pre.'&'.$end, $pre.'"'.$end, $pre.''.$end), $string);
  12. $strcut = '';
  13. if(strtolower($charset) == 'utf-8') {
  14. $n = $tn = $noc = 0;
  15. while($n $t = ord($string[$n]);
  16. if($t == 9 || $t == 10 || (32 $tn = 1; $n++; $noc++;
  17. } elseif(194 $tn = 2; $n += 2; $noc += 2;
  18. } elseif(224 $tn = 3; $n += 3; $noc += 2;
  19. } elseif(240 $tn = 4; $n += 4; $noc += 2;
  20. } elseif(248 $tn = 5; $n += 5; $noc += 2;
  21. } elseif($t == 252 || $t == 253) {
  22. $tn = 6; $n += 6; $noc += 2;
  23. } else {
  24. $n++;
  25. }
  26. if($noc >= $length) {
  27. break;
  28. }
  29. }
  30. if($noc > $length) {
  31. $n -= $tn;
  32. }
  33. $strcut = substr($string, 0, $n);
  34. } else {
  35. for($i = 0; $i $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
  36. }
  37. }
  38. //还原特殊字符串
  39. $strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.''.$end), array('&', '"', ''), $strcut);
  40. //修复出现特殊字符串截段的问题
  41. $pos = strrpos($s, chr(1));
  42. if($pos !== false) {
  43. $strcut = substr($s,0,$pos);
  44. }
  45. return $strcut.$dot;
  46. }
  47. ?>
复制代码

以上就是php摘要生成函数的完整代码,大家复制到自己的编辑器中,然后测试运行吧。



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