Re: Substr and HTML tags - Problem...
| From: | Simon Willison | Date: | Mon, 25 Feb 2002 11:41:13 +0000 |
| Subject: | Re: Substr and HTML tags - Problem... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-86067@lists.php.net to get a copy of this message | ||
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 webpage... 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.