wp_html_excerpt

The timeline below displays how wordpress function wp_html_excerpt has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Safely extracts not more than the first $count characters from HTML string.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param string $str   String to get the excerpt from.
 * @param int    $count Maximum number of characters to take.
 * @param string $more  Optional. What to append if $str needs to be trimmed. Defaults to empty string.
 * @return string The 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;
}

WordPress Version: 5.5

/**
 * Safely extracts not more than the first $count characters from HTML string.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param string $str   String to get the excerpt from.
 * @param int    $count Maximum number of characters to take.
 * @param string $more  Optional. What to append if $str needs to be trimmed. Defaults to empty string.
 * @return string The 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;
}

WordPress Version: 5.4

/**
 * Safely extracts not more than the first $count characters from html string.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param string $str   String to get the excerpt from.
 * @param int    $count Maximum number of characters to take.
 * @param string $more  Optional. What to append if $str needs to be trimmed. Defaults to empty string.
 * @return string The 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;
}

WordPress Version: 4.3

/**
 * Safely extracts not more than the first $count characters from html string.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param string $str   String to get the excerpt from.
 * @param int    $count Maximum number of characters to take.
 * @param string $more  Optional. What to append if $str needs to be trimmed. Defaults to empty string.
 * @return string The 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;
}

WordPress Version: 3.7

/**
 * Safely extracts not more than the first $count characters from html string.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param string $str String to get the excerpt from.
 * @param integer $count Maximum number of characters to take.
 * @param string $more Optional. What to append if $str needs to be trimmed. Defaults to empty string.
 * @return string The 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;
}