/**
* 截取文章或详情为摘要
* @param $content 内容
* @param $count 摘要的字数
* */
function getDescriptionFromContent($content, $count)
{
$content = preg_replace("@<script(.*?)</script>@is", "", $content);
$content = preg_replace("@<iframe(.*?)</iframe>@is", "", $content);
$content = preg_replace("@<style(.*?)</style>@is", "", $content);
$content = preg_replace("@<(.*?)>@is", "", $content);
$content = str_replace('\n', '', $content);
$content = str_replace('\r', '', $content);
$content = str_replace('\r\n', '', $content);
// $content = str_replace(PHP_EOL, '', $content);
$space = array(" ", " ", " ", " ", " ");
$go_away = array("", "", "", "", "");
$content = str_replace($space, $go_away, $content);
$res = mb_substr($content, 0, $count, 'UTF-8');
if (mb_strlen($content, 'UTF-8') > $count) {
$res = $res . "...";
}
return trim($res,'"');
}
ThinkPHP6 获取 摘要
最新推荐文章于 2024-11-09 10:36:36 发布
该函数主要用于从文章内容中提取指定字数的摘要,首先通过正则表达式移除HTML的<script>、<iframe>和<style>标签,再删除其他所有HTML标签,然后进行换行符和空格的替换,确保内容整洁。最后使用mb_substr获取指定长度的UTF-8编码的摘要,并在超出长度时添加省略号。
摘要由CSDN通过智能技术生成