ZF-4734: Create a Zend_Filter_StringTruncate


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.


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 ....

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?

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.

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.

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.

anyone have any code for this?

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.

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.

Proposal waiting for recommendation from the devteam since 15.04.2009

Closing as "Needs Proposal".

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