wp_html_excerpt()
Safely extracts the specified number of characters from the specified HTML string.
What the function does:
- Strips all HTML tags.
- Trims the string to the specified number of characters (safe for UTF-8 characters).
Entities inside will NOT be counted as one character. For example, & will be counted as 5, < as 4, etc.
Use wp_trim_words() when you need to trim a string to a specified number of words.
Uses: wp_strip_all_tags()
1 time — 0.0000191 sec (very fast) | 50000 times — 0.10 sec (speed of light) | PHP 7.4.25, WP 6.0.1
No Hooks.
Returns
String. The trimmed string.
Usage
wp_html_excerpt( $str, $count, $more );
- $str(string) (required)
- The string to be trimmed.
- $count(int) (required)
- How many characters to trim the specified string to.
- $more(string)
The string to append at the end if the specified text is trimmed (more than the specified number of characters).
Usually, an ellipsis is added here:
...or its symbol….Default: empty string
Examples
Changelog
| Since 2.5.0 | Introduced. |
wp_html_excerpt() wp html excerpt code WP 7.0
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;
}