RE: [PHP] Substr and HTML tags - Problem...

2002-02-25 Thread Jon Haworth

> 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 :)
> 

You don't have a handy way to check that your substr() doesn't cut off
half-way through a word, do you?

I'm up against this issue myself at the moment and it's a PITA to sort out -
I keep getting things like "Fnord fnord fnord fnord fnord fno", where the
last "fnord" is missing its "rd".

It's bound to be a case of stepping back through the string until a space is
encountered, and then keeping everything to the left of that, but I haven't
worked out an elegant way to do it yet :-)


Cheers
Jon


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Substr and HTML tags - Problem...

2002-02-25 Thread Simon Willison

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  and this tag
>will be cut by the substr to something like that: 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.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php