acf_get_truncated()ACF 5.0.0

This function will truncate and return a string

No Hooks.

Returns

String.

Usage

acf_get_truncated( $text, $length );
$text(string) (required)
The text to truncate.
$length(int)
The number of characters to allow in the string.
Default: 64

Changelog

Since 5.0.0 Introduced.

acf_get_truncated() code ACF 6.8.8

function acf_get_truncated( $text, $length = 64 ) {
	$text       = trim( $text );
	$the_length = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );

	$cut_length = $length - 3;
	$return     = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $cut_length ) : substr( $text, 0, $cut_length );

	if ( $the_length > $cut_length ) {
		$return .= '...';
	}

	return $return;
}