首頁  >  文章  >  後端開發  >  thinkphp截取字串函數無法顯示省略號怎麼解決?

thinkphp截取字串函數無法顯示省略號怎麼解決?

WBOY
WBOY原創
2016-07-25 09:11:52746瀏覽

对于thinkphp的截取字符串函数无法显示省略号的情况,可以参考如下解决方法: 打开common/extend.php页面,修改msubstr函数为:

  1. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
  2. {
  3. if(function_exists("mb_substr")) {
  4. if($suffix)
  5. {
  6. if($str==mb_substr($str, $start, $length, $charset))
  7. {
  8. return mb_substr($str, $start, $length, $charset);
  9. }
  10. else
  11. {
  12. return mb_substr($str, $start, $length, $charset)."...";
  13. }
  14. } // bbs.it-home.org
  15. else
  16. {
  17. return mb_substr($str, $start, $length, $charset);
  18. }
  19. }
  20. elseif(function_exists('iconv_substr')) {
  21. if($suffix)
  22. {
  23. if($str==iconv_substr($str,$start,$length,$charset))
  24. {
  25. return iconv_substr($str,$start,$length,$charset);
  26. }
  27. else
  28. {
  29. return iconv_substr($str,$start,$length,$charset)."...";
  30. }
  31. }
  32. else
  33. {
  34. return iconv_substr($str,$start,$length,$charset);
  35. }
  36. }
  37. $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
  38. $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
  39. $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
  40. $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
  41. preg_match_all($re[$charset], $str, $match);
  42. $slice = join("",array_slice($match[0], $start, $length));
  43. if($suffix) return $slice."…";
  44. return $slice;
  45. }
复制代码

使用以上函数在截取字符串时,就可以显示省略号了,大家可以测试下看看效果如何。



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn