$maxLength && ! empty( $maxLength ) && ! empty( $text ) ){ return function_exists( 'mb_strimwidth' ) ? mb_strimwidth( $text, 0, $maxLength, '' ) . $more : substr( $text, 0, $maxLength ) . $more; } return $text; } /** * Trims text to a certain number of words. * * @param string $text The text to be trimmed * @param int $maxLength Number of words. * @param string $more What to append if $text needs to be trimmed. Default ' …'. * * @return mixed|string */ public static function trimByWords( $text, $maxLength, $more = " ..." ) { $words = preg_split('/\s+/u', $text, $maxLength + 1); if( count( $words ) > $maxLength ) { array_pop($words); $text = implode(' ', $words); $text .= $more; } return $text; } }