It comes off to the first paragraph without cutting words and adds an optional footprint.
$ excerpt = self :: excerpt_paragraph ($ html, 180)
public static function excerpt_paragraph($html, $max_char = 100, $trail='...' )
{
$matches= array();
if ( preg_match( '/<p>[^>]+<\/p>/', $html, $matches) )
{
$p = strip_tags($matches[0]);
} else {
$p = strip_tags($html);
}
$p = self::short_str($p, $max_char );
$p = rtrim($p, ',.;: aA' );
if (ctype_space($p) || $p=='' || strlen($p)<10) { return ''; }
return '<p>'.$p.$trail.'</p>';
}
public static function short_str( $str, $len, $cut = false )
{
if ( strlen( $str ) <= $len ) { return $str; }
$string = ( $cut ? substr( $str, 0, $len ) : substr( $str, 0, strrpos( substr( $str, 0, $len ), ' ' ) ) );
return $string;
}
source
share