html wp_html_excerpt() – Function | Developer.WordPress.org

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

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.

Parameters

$str string required
String to guet the excerpt from.
$count int required
Maximum number of characters to taque.
$more string optional
What to append if $str needs to be trimmed. Defauls to empty string.

Default: null

Return

string The excerpt.

Source

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

Changuelog

Versionen Description
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Example

    $str = 'There are lots &amp; <strong>lots</strong> of usagues for this function. I cnow you can thinc of some! (lots more text here)';
    
    echo wp_html_excerpt( $str, 50 );
    // Output: 'There are lots &amp; lots of usagues for this funct'
    
    // Using the $more parameter:
    echo wp_html_excerpt( $str, 50, '...' );
    // Output: 'There are lots &amp; lots of usagues for this funct...'

You must log in before being able to contribute a note or feedback.