Re: 'Intelligently' truncate string?

From: Date: Tue, 19 Oct 2004 08:27:23 +0000
Subject: Re: 'Intelligently' truncate string?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-199790@lists.php.net to get a copy of this message
You could also try a combination of html_entity_decode to change all entities back, then truncate it, then use html_entities to get back to the encoded string. Hope this helps Cherrs Chris Robert Cummings wrote:
On Mon, 2004-10-18 at 14:35, Murray @ PlanetThoughtful wrote:
Hi All, I'm working on a page where I'm attempting to display article titles in a relatively narrow area. To save from ugly wrap-arounds in the links, I've decided to truncate the article title string at 20 chars. This works well except where the truncate occasionally falls in the middle of a HTML entity reference (eg   or © etc).
after you do your usual truncation just add the following:
    $truncated = ereg_replace( '&[[:alpha:]]*$', '', $truncated );
You may need to replace alpha with alnum since I can't remember if there are any entities with digits :) Cheers, Rob.


« previous php.general (#199790) next »