wp_html_excerpt

函数
wp_html_excerpt ( $str, $count, $more = null )
参数
  • (string) $str String to get the excerpt from.
    Required:
  • (int) $count Maximum number of characters to take.
    Required:
  • (string) $more Optional. What to append if $str needs to be trimmed. Defaults to empty string.
    Required:
    Default: null
返回值
  • (string) The excerpt.
定义位置
相关方法
wp_trim_excerptwp_html_splithas_excerptthe_excerptwp_embed_excerpt_more
引入
2.5.0
弃用
-

wp_html_excerpt: 这个函数用于在保留标签的同时截断HTML内容。它把内容、长度和更多的文本作为参数,并返回截断后的内容。

安全地从HTML字符串中提取不超过前$count的字符。

UTF-8,标签和实体安全提取前缀。里面的实体将*NOT*被算作一个字符。例如,&会被算作4,<会被算作3,等等。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_html_excerpt( $str, $count, $more = null ) {
if ( null === $more ) {
$more = '';
}
$str = wp_strip_all_tags( $str, true );
$excerpt = mb_substr( $str, 0, $count );
// Remove part of an entity at the end.
$excerpt = preg_replace( '/&[^;s]{0,6}$/', '', $excerpt );
if ( $str != $excerpt ) {
$excerpt = trim( $excerpt ) . $more;
}
return $excerpt;
}
function wp_html_excerpt( $str, $count, $more = null ) { if ( null === $more ) { $more = ''; } $str = wp_strip_all_tags( $str, true ); $excerpt = mb_substr( $str, 0, $count ); // Remove part of an entity at the end. $excerpt = preg_replace( '/&[^;s]{0,6}$/', '', $excerpt ); if ( $str != $excerpt ) { $excerpt = trim( $excerpt ) . $more; } return $excerpt; }
function wp_html_excerpt( $str, $count, $more = null ) {
	if ( null === $more ) {
		$more = '';
	}

	$str     = wp_strip_all_tags( $str, true );
	$excerpt = mb_substr( $str, 0, $count );

	// Remove part of an entity at the end.
	$excerpt = preg_replace( '/&[^;s]{0,6}$/', '', $excerpt );
	if ( $str != $excerpt ) {
		$excerpt = trim( $excerpt ) . $more;
	}

	return $excerpt;
}

常见问题

FAQs
查看更多 >