ap_truncate_chars( string $text, int $limit = 40, string $ellipsis = '...' )

Description #

Truncate string but preserve full word.

Parameters #

  • $text
    string (Required) String.
  • $limit
    int (Optional) Limit string to. Default value: 40
  • $ellipsis
    string (Optional) Ellipsis. Default value: '...'

Changelog #

VersionDescription
4.1.8Introduced.

Source #

File: includes/functions.php

function ap_truncate_chars( $text, $limit = 40, $ellipsis = '...' ) {
	$text = wp_strip_all_tags( $text );
	$text = str_replace( array( "\r\n", "\r", "\n", "\t" ), ' ', $text );
	if ( strlen( $text ) > $limit ) {
		$endpos = strpos( $text, ' ', (string) $limit );

		if ( false !== $endpos ) {
			$text = trim( substr( $text, 0, $endpos ) ) . $ellipsis;
		}
	}
	return $text;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment