php 文字过长变省略号

55 阅读1分钟

image.png

<?php 
  class MsubStr{
    public function csubstr($str,$start,$len){
      $strLen = $start + $len;
      $tmpstr = '';
      for($i = $start; $i < $strLen; $i++){
        if(ord(substr($str, $i, 1)) > 0xa0){
          $tmpstr .= substr($str, $i, 2);
          $i++;
        }
        else{
          $tmpstr .= substr($str, $i, 1);
        }
      }
      return $tmpstr;
    }
  }
  $substr = new MsubStr();
  $string = "人们常说,你未来的模样,就藏在你现在的努力里。";
  if(strlen($string) > 10){
    echo $substr->csubstr($string,0,11).'...';
  }
?>