Skip to end of metadata
Go to start of metadata

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[

Zend Framework: Zend_Filter_StringLength Component Proposal

Proposed Component Name Zend_Filter_StringLength
Developer Notes
Proposers Thomas Weidner
Zend Liaison TBD
Revision 1.0 - 12 April 2009: Initial Draft
1.1 - 13 April 2009: added View Helper (wiki revision: 8)

Table of Contents

1. Overview

Zend_Filter_StringLength is a filter which allows to pad/extend or truncate a string to a given length. It will also have a view helper which allows to use this filter easily within your view.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component will pad a string to a given length
  • This component will allow to pad/extend a string on both sides
  • This component will allow to use any character to pad the string
  • This component will truncate a string at a given length
  • This component will only work on strings

4. Dependencies on Other Framework Components

  • Zend_Filter
  • Zend_View_Helper

5. Theory of Operation

This component allows to pad/extend a string to a given length the same way as str_pad would do. And to truncate a string to a given length as substr allows to.

It will additionally allow to seperate text based on words by using eighter whitespaces, hypens or any other character.

And it adds a view helper which allows to use this filter within your view. The View Helper will also allow to set the filter within a registry using the name 'Zend_Filter_StringLength'.

6. Milestones / Tasks

  • Milestone 1: [DONE] Proposal finished
  • Milestone 2: Proposal accepted
  • Milestone 3: Working implementation
  • Milestone 4: Unit tests
  • Milestone 5: Documentation
  • Milestone 6: Moved to core

7. Class Index

  • Zend_Filter_StringLength
  • Zend_View_Helper_StringLength

8. Use Cases


Pad a string to 15 chars


Pad a string to 15 chars, using another pad char


Pad a string to 15 chars, using another pad char and another direction


Truncate a string at 6 chars


Truncate a string at 6 chars, using ending chars


Define padding and truncating at the same time


Define truncating based on words


Using the viewhelper


Define truncating based on different trunc-characters


Using the viewhelper with registry

9. Class Skeletons



Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Apr 12, 2009

    <p>For padding multibyte strings (e.g. utf-8) this filter should use Zend_Text_MultiByte::strPad() instead of PHP's str_pad() function.</p>

    1. Apr 13, 2009

      <p>I never said that it will use internally php's str_pad <ac:emoticon ac:name="wink" /></p>

      1. Apr 13, 2009

        <p>So what will it use then?</p>

        1. Apr 13, 2009

          <p>Ben:<br />
          The point is, that actually there exists no code... only in my brain.<br />
          So the details of which class/extension/component to use for this sub-task (there are several sub-tasks which have to be done) will be decided when I evaluate how this filter should work internally (beta-class).</p>

          <p>It can use this component or another... I have not decided or fixed any decission for now. This will be done and discussed when this proposal has been approved in general.</p>

  2. Apr 12, 2009

    <p>Looks like a great component!</p>

    <p>I do have a first questions though;<br />
    1. How will this handle multibyte strings?<br />
    2. I would like an option that ensures words themself to not get cut somewhere half way. On IRC you told me to look at UC5, but obviously it isn't in that usecase?<br />
    3. Will you add a viewhelper along with this filter? It seems to me that a lot of users want to 'cut strings' when presenting the data, rather than doing it while inserting?</p>

    1. Apr 13, 2009

      <p>1.) As normal strings, this component will also handle multibyte strings</p>

      <p>2.) You didn't speak of words, but of a way to have not only a single character added when truncating. Truncating words is possible, but will use a seperate constant. I'll add a new usecase. But this possibility is limited to truncate based on whitespaces, and not one word in two seperated because there is no way to define a exact rule where a word has to be seperated</p>

      <p>3.) No, because this is a seperate proposal and has nothing to do with this filter. As soon as this filter is available we can simply add a view helper using this filter.</p>

      1. Apr 13, 2009

        <p>I'd argue that adding a view helper that consumes this filter should be part of this proposal. It's a common use case for the filter.</p>

        1. Apr 13, 2009

          <p>Are you sure ?</p>

          <p>Because a view helper is another component theoretical independent from this filter (or this component independently from the view helper <ac:emoticon ac:name="smile" /> )</p>

          1. Apr 14, 2009

            <p>I`d agree with Matthew , It is a popular use case to use it in a view <br />
            see <a class="external-link" href=""></a></p>

            1. Apr 14, 2009

              <p>Probably... but as this is already part of this proposal since more than a day, what's the reason of this additional comment ?</p>

              1. Apr 14, 2009

                <p>Sorry for that , I didn`t notice change</p>

                1. Apr 14, 2009

                  <p>No problem</p>

  3. May 03, 2009

    <p>I think it would be better to split this in two filters:<br />
    Zend_Filter_StringPad<br />

    <p>than it is selectable if you would only truncate or pad a string:</p>

    <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
    // truncate a string if it is longer than length and add fill
    $filter = new Zend_Filter_StringTruncate(array('length' => 10, 'fill' => '...', 'type' => Zend_Filter_StringTruncate::PAD_RIGTH));
    $filter->filter('This is a test'); // This is...

    // pad a string if it is smaller than length
    $filter = new Zend_Filter_StringPad(array('length' => 10, 'fill' => '.', 'type' => Zend_Filter_StringPad::PAD_RIGTH));
    $filter->filter('This is a test'); // This is a test

    // both and pad left
    $filter = new Filter();
    $filter->addFilter(new Zend_Filter_StringTruncate(array('length' => 10, 'fill' => '...', 'type' => Zend_Filter_StringTruncate::PAD_RIGTH)));
    $filter->addFilter(new Zend_Filter_StringPad(array('length' => 20, 'fill' => '.', 'type' => Zend_Filter_StringPad::PAD_LEFT)));
    $filter->filter('This is a test'); // ..........This is...

    1. May 03, 2009

      <p>It is already possible to solve this with the above described API.</p>

      <p>You can eighter pad or truncate or do both actions with the same filter.<br />
      You can also use two filter instances and pad on both sides with different length as described by your example, even if I would not see a real usecase for such a task.</p>

      1. Jun 08, 2009


  4. Jul 14, 2009

    <ac:macro ac:name="note"><ac:parameter ac:name="title">Declined in current state</ac:parameter><ac:rich-text-body>
    <p>The Zend Framework team declines acceptance of this proposal in its current state. The component is poorly named, and perhaps performs too many different, discrete tasks. From our review, it:</p>
    <li>Pads a string (StringPad would be a good name)</li>
    <li>Truncates a string (StringTruncate would be a good name)</li>
    <li>Truncates based on words (WordTruncate would be a good name)<br />
    Our recommendation is that these be three distinct filters. Developers may chain them together to get the combined behavior shown in some of the use cases.</li>

    <p>Please re-submit the proposal once you have made the above changes.</p></ac:rich-text-body></ac:macro>

    1. Jul 23, 2009

      <p>Question:<br />
      Does this also mean to add 3 different view helpers ? One for each filter ?</p>