lp__trim_chars( $text, $max_chars = 150, $more = null )

This function trims the provided text to the specified maximum character count while preserving formatting and adding an ellipsis if the text is truncated.

Plugin

Namespace

Launchpad

Parameters

string

$text

The original text to trim.

string

An optional string to append to the end of the trimmed text, e.g., ….

int

$max_chars

The maximum number of characters to trim the text to. Default is 150.

Return

string

The trimmed text.

Source

File: \includes\lp-utility-functions.php

function lp__trim_chars($text, $max_chars = 150, $more = null) {
        // Check if the optional "more" string is provided; if not, use an ellipsis.
        if (null === $more) {
            $more = __('…');
        }

        $original_text = $text;
        $text = wp_strip_all_tags($text);
        $max_chars = (int) $max_chars;

        // Check if the length of the text exceeds the maximum character count.
        if (strlen($text) > $max_chars) {
            // Trim the text to the specified character count, ensuring proper handling of UTF-8.
            $text = mb_substr($text, 0, $max_chars, 'UTF-8') . $more;
        }

        /**
         * Filters the text content after characters have been trimmed.
         *
         * @param string $text          The trimmed text.
         * @param int    $max_chars     The maximum number of characters to trim the text to. Default 100.
         * @param string $more          An optional string to append to the end of the trimmed text, e.g., ….
         * @param string $original_text The text before it was trimmed.
         */
        return apply_filters('lp__trim_chars', $text, $max_chars, $more, $original_text);
    }

Related

Use to convert the first character of each word in a string to uppercase.

Use to format a date string according to the specified format and timezone.

Use to escape single quotes in a string.