Re: 'Intelligently' truncate string?

From: Date: Mon, 18 Oct 2004 18:53:06 +0000
Subject: Re: 'Intelligently' truncate string?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-199756@lists.php.net to get a copy of this message
On Tue, 19 Oct 2004 04:35:21 +1000, Murray @ PlanetThoughtful <lists@planetthoughtful.org> wrote: > 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 &nbsp; or &copy; etc). > > I'm wondering if anyone else on the list has worked out a way of > intelligently truncating a string to take these kinds of occurrences into > account? I don't mind, in these situations if the truncation takes place > before or after the entity, since when displayed it will only equate to one > character more or less. You can use wordwrap() to do intelligent line breaks, then you can truncate the new string based on a line break. You can also explode() the string into an array then rebuild a new string with only so many elements of the array. -- Greg Donald Zend Certified Engineer http://gdconsultants.com/ http://destiney.com/

« previous php.general (#199756) next »