Page 1 of 1

html substr

Posted: Wed Nov 24, 2010 10:15 am
by pedroz
I found the following function on internet to html crop.
It is almost working well...

Code: Select all

print html_substr("<p>my </p><p><a href='http://www.google.com''>markeup</a></p> <p><b>text</b></p>", 0, 5 ); // <p>my </p><p><a href='http://www.google.com''>ma</a> <p><b></b></p>
How can I remove all <p><b></b></p> tags?
Basically after the limit, it is showing all tags without text.
It should not be appearing..

Thanks

Code: Select all

print html_substr("<p>my </p><p><a href='http://www.google.com''>markeup</a></p> <p><b>text</b></p>", 0, 5 );


function html_substr( $s, $srt, $len = NULL, $strict=false, $suffix = NULL )
{
	if ( is_null($len) ){ $len = strlen( $s ); }
	
	$f = 'static $strlen=0; 
			if ( $strlen >= ' . $len . ' ) { return "><"; } 
			$html_str = html_entity_decode( $a[1] );
			$subsrt   = max(0, ('.$srt.'-$strlen));
			$sublen = ' . ( empty($strict)? '(' . $len . '-$strlen)' : 'max(@strpos( $html_str, "' . ($strict===2?'.':' ') . '", (' . $len . ' - $strlen + $subsrt - 1 )), ' . $len . ' - $strlen)' ) . ';
			$new_str = substr( $html_str, $subsrt,$sublen); 
			$strlen += $new_str_len = strlen( $new_str );
			$suffix = ' . (!empty( $suffix ) ? '($new_str_len===$sublen?"'.$suffix.'":"")' : '""' ) . ';
			return ">" . htmlentities($new_str, ENT_QUOTES, "UTF-8") . "$suffix<";';
	
	return preg_replace( array( "#<[^/][^>]+>(?R)*</[^>]+>#", "#(<(b|h)r\s?/?>){2,}$#is"), "", trim( rtrim( ltrim( preg_replace_callback( "#>([^<]+)<#", create_function(
            '$a',
          $f
        ), ">$s<"  ), ">"), "<" ) ) );
}
die();

Re: html substr

Posted: Thu Nov 25, 2010 1:51 pm
by Celauran
It's not clear to me what it is you want to do that strip_tags() doesn't already do. Can you give a sample input with the desired output?