php提取富文本中字符串的纯文本并进行字符字数截取和清理换行空格

醉菜鸟 2022-06-26 08:00:06

/**
 * 获取富文本字符串的纯文本,截取
 * @param $string
 * @param int $number
 * @return string|null
*/
function StringToText($string,$number = 120): string
{
   if($string){
      $html_string = htmlspecialchars_decode($string);
      $content = str_replace([" ","\t","\n","\r"], "", $html_string);
      $contents = strip_tags($content);
      return mb_strlen($contents,'utf-8') > $number ? mb_substr($contents, 0, $number, "utf-8").'...' : mb_substr($contents, 0, $number, "utf-8");
    }else{
      return $string;
    }
}
意见反馈