Sascha Ragtschaa wrote

>I need to limit a teaser-text via substr($teaser,0,100). The Problem I now
>have is, if the last 4 string chars are a html tag like <br> and this tag
>will be cut by the substr to something like that: <br - it will mess up the
>How can I avoid that the html tags are cut by the substr function? Is there
>a way to make the function know, that it has to leave html tags uncut??
Personally when I'm doing teasers I strip all HTML from the content 
before using substr:

$teaser = strip_tags($content);
$teaser = substr($teaser, 0, 100);
$teaser .= '...';     // Nice touch to have a '...' on the end :)

I can't think of a way of keeping HTML tags but ensuring you don't cut 
off half way through one - I suppose you could do it by scanning along 
the string character by character keeping track ofwhether or not you 
have encountered a < without a matching >, then if you get to the end 
and you are half way through a tag running back to the start of the tag 
and deleting from there.

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to