php 从提供的内容中生成摘录。剥离HTML,删除尾随标点符号,并在删除文本时添加“更多”字符串。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 从提供的内容中生成摘录。剥离HTML,删除尾随标点符号,并在删除文本时添加“更多”字符串。相关的知识,希望对你有一定的参考价值。

<?php

/**
 * Get an excerpt
 *
 * @param string $content The content to be transformed
 * @param int    $length  The number of words
 * @param string $more    The text to be displayed at the end, if shortened
 * @return string
 */
function get_excerpt( $content, $length = 40, $more = '...' ) {
	$excerpt = strip_tags( trim( $content ) );
	$words = str_word_count( $excerpt, 2 );
	if ( count( $words ) > $length ) {
		$words = array_slice( $words, 0, $length, true );
		end( $words );
		$position = key( $words ) + strlen( current( $words ) );
		$excerpt = substr( $excerpt, 0, $position ) . $more;
	}
	return $excerpt;
}

以上是关于php 从提供的内容中生成摘录。剥离HTML,删除尾随标点符号,并在删除文本时添加“更多”字符串。的主要内容,如果未能解决你的问题,请参考以下文章

PHP - 从请求中生成干净的 TXT 输出

PHP DOM获取nodevalue html? (不剥离标签)

为啥 echo 语句没有在我的 PHP 代码中生成任何内容?

php 从摘录中删除短代码(WordPress)

在laravel中生成视图时如何使php vars保持不变?

从 WKWebview 中删除/剥离 HTML 类