artikus11
4/2/2018 - 8:58 AM

Обрезка текста

Обрезка текста

// Truncate string to x letters/words
function get_truncate( $str, $length = 40, $units = 'letters', $ellipsis = ' …' ) {
	$str = preg_replace ('~\[/?.*?\](?!\()~', '', $str );
	if ( $units == 'letters' ) {
		if ( mb_strlen( $str ) > $length ) {
			return mb_substr( $str, 0, $length ) . $ellipsis;
		} else {
			return $str;
		}
	} else {
		$words = explode( ' ', $str );
		if ( count( $words ) > $length ) {
			return implode( " ", array_slice( $words, 0, $length ) ) . $ellipsis;
		} else {
			return $str;
		}
	}
}