ZF-4734: Create a Zend_Filter_StringTruncate

Issue Type: New Feature Created: 2008-10-28T13:37:30.000+0000 Last Updated: 2009-12-07T15:11:15.000+0000 Status: Resolved Fix version(s): - 1.10.0 (27/Jan/10)

Reporter: Bradley Holt (bradley.holt) Assignee: Thomas Weidner (thomas) Tags: - Zend_Filter

Related issues: Attachments:


Please create a Zend_Filter_StringTruncate class that will truncate a given string to a certain number of characters or words.

For example, I want to truncate the following:

The quick brown fox jumped over the lazy sleeping dog.

to 30 characters:

The quick brown fox jumped ove

or to 30 characters, but the closest whole word:

The quick brown fox jumped

or to 6 words (instead of characters):

The quick brown fox jumped over

This is a common filter that is needed and many developers end up implementing their own PHP code for this. It would be nice to have a standard way of truncating strings within ZF.


Posted by Bruno Friedmann (brunofriedmann) on 2008-10-29T04:45:02.000+0000

Please get it directly ready for utf-8 string php5 ( mb present or not ) php6 ready.

If I want to substr to 10 chars the ça aide chaque développeur à se faire son opinion ....

Posted by Goran Juric (gog) on 2008-10-29T09:57:23.000+0000

This filter should take two optional parameters:

1.) A string to append to the truncated string, something like '…', ' more', etc. (this one should be null by default) 2.) option to append 1.) to the strings that wasn't truncated as well

Maybe combine the url view helper, so 1.) can be a link?

Posted by Bradley Holt (bradley.holt) on 2008-10-29T10:08:37.000+0000

I agree with Bruno that this should treat UTF-8 double byte characters as single characters. Goran, those two optional parameters are good ideas as well.

Also, I am now questioning the usefulness of being able to truncate based on word count. I would not be disappointed if that were not a feature of this filter.

Posted by Goran Juric (gog) on 2008-10-29T14:46:29.000+0000

Agree about the wordcount. Strings are truncated because you need to fit them in a given space. Word count in this case means nothing at all.

Posted by Luke Barton (ziro) on 2008-12-10T06:24:11.000+0000

I think the wordcount and round to nearest word end features should be considered. If you're going to implement something, it would be nice to have those features too.

Posted by Ralph Schindler (ralph) on 2009-01-11T21:48:11.000+0000

anyone have any code for this?

Posted by Thomas Weidner (thomas) on 2009-01-11T23:03:57.000+0000

Ralph: You can find a WordCount filter for files under Zend_Filter_File_WordCount in trunk. Extract the file handling and you have a WordCount filter.

Of course we should then also change the file filter also to use the new WordCount Filter.

Posted by Thomas Weidner (thomas) on 2009-01-11T23:07:54.000+0000

Regarding mbstring...

We clearly have to use iconv as it's installed per default in PHP. This practice is also used in most other ZF classes.

We are only allowed to use mbstr* when there is no other way. Most things can be solved by using binaryaware string functions and iconv.

Posted by Thomas Weidner (thomas) on 2009-07-09T04:51:05.000+0000

Proposal waiting for recommendation from the devteam since 15.04.2009

Posted by Thomas Weidner (thomas) on 2009-12-07T15:11:15.000+0000

Closing as "Needs Proposal".

The related proposal has already been written. See here for details and vote for it:…

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.