过滤文章摘要

概念:

过滤文章摘要指从一篇文章中提取出一段简洁、准确的文字描述,通常用于展示文章的概要信息。在进行文章摘要过滤时,需要考虑文章主题、关键词、句子结构和语言表达等因素,以确保生成的摘要能够准确、完整地反映文章内容。通常情况下,过滤文章摘要的工作会交由自然语言处理技术实现。

可以使用TP6的封装函数mb_substr实现过滤文章摘要的功能,同时可以通过自定义函数来封装该功能,以便在整个应用程序中复用。

以下是一个示例代码,其中filterSummary函数接受文章内容和摘要长度作为参数,并返回过滤后的文章摘要。

use think\helper\Str;

/**
 * 过滤文章摘要
 * @param string $content 文章内容
 * @param int $length 摘要长度
 * @return string 过滤后的文章摘要
 */
function filterSummary(string $content, int $length): string
{
    // 过滤HTML标签
    $content = Str::removeHtmlTag($content);

    // 截取指定长度的字符串作为摘要
    return mb_substr($content, 0, $length);
}
use app\utils\TextUtil;

// 获取文章列表
$articles = Article::select();

// 遍历文章列表,过滤文章摘要
foreach ($articles as $article) {
    $summary = filterSummary($article->content, 200);
    // ...
}

前端显示

<div><a href="">{$art.content|getArtContent}</a></div>

显示结果

猜你喜欢

转载自blog.csdn.net/MrWangisgoodboy/article/details/129832130