wp_html_excerpt() WordPress Function

The wp_html_excerpt() function allows you to create an excerpt for a post or page that includes HTML tags. This can be useful if you want to include some basic formatting in your excerpt, such as bold or italic text.

wp_html_excerpt( string $str, int $count, string $more = null ) #

Safely extracts not more than the first $count characters from HTML string.


Description

UTF-8, tags and entities safe prefix extraction. Entities inside will NOT be counted as one character. For example & will be counted as 4, < as 3, etc.


Top ↑

Parameters

$str

(string)(Required)String to get the excerpt from.

$count

(int)(Required)Maximum number of characters to take.

$more

(string)(Optional) What to append if $str needs to be trimmed. Defaults to empty string.

Default value: null


Top ↑

Return

(string) The excerpt.


Top ↑

Source

File: wp-includes/formatting.php

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;
}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More