wp_html_excerpt

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

Description

(string) wp_html_excerpt( (string) $str, (number) $count, (null) $more = null ); 

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.

Returns (string)

The excerpt.

Parameters (3)

0. $str (string)
String to get the excerpt from.
1. $count (number)
Maximum number of characters to take.
2. $more — Optional. (null) => null
What to append if $str needs to be trimmed. Defaults to empty string.

Usage

  1. if ( !function_exists( 'wp_html_excerpt' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // String to get the excerpt from. 
  5. $str = ''; 
  6.  
  7. // Maximum number of characters to take. 
  8. $count = null; 
  9.  
  10. // Optional. What to append if $str needs to be trimmed. Defaults to empty string. 
  11. $more = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_html_excerpt($str, $count, $more); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function wp_html_excerpt( $str, $count, $more = null ) { 
  2. if ( null === $more ) 
  3. $more = ''; 
  4. $str = wp_strip_all_tags( $str, true ); 
  5. $excerpt = mb_substr( $str, 0, $count ); 
  6. // remove part of an entity at the end 
  7. $excerpt = preg_replace( '/&[^;\s]{0, 6}$/', '', $excerpt ); 
  8. if ( $str != $excerpt ) 
  9. $excerpt = trim( $excerpt ) . $more; 
  10. return $excerpt;