ZF-2348: Zend_View_Helper_Paragraph

Issue Type: New Feature Created: 2007-12-22T10:33:01.000+0000 Last Updated: 2009-02-08T03:02:39.000+0000 Status: Closed Fix version(s): - 1.8.0 (30/Apr/09)

Reporter: Joó Ádám (ceriak) Assignee: Ralph Schindler (ralph) Tags: - Zend_View

Related issues: Attachments:


A new view helper, intended to replace nl2br() - ease and propagate the use of the semantically meaningful


p> element instead of


  • trim()-s the supplied text.
  • Deletes all carriage returns
  • Searches for all (possibly multiple) newlines, and replaces them with a closing and an opening p element (using preg_replace()).
  • Wraps the result into an opening and a closing p tag and returns.


<pre class="highlight">class Zend_View_Helper_Paragraph
    public function paragraph($text)
        return '

' . preg_replace('/[\r\n]+(\h+[\r\n]+)?/', '

```', trim($text)) . '`



In your view:

<pre class="highlight">
echo $this->paragraph($this->text);


Posted by Joó Ádám (ceriak) on 2007-12-26T20:23:36.000+0000

Fixing a bug by eliminating carriage returns.

Posted by Jon Whitcraft (sidhighwind) on 2008-01-07T18:17:35.000+0000

What about something like this?

<pre class="highlight">function wpautop($pee, $br = 1) {
    $pee = $pee . "\n"; // just to make things a little easier, pad the end
    $pee = preg_replace('|<br></br>\s*<br></br>|', "\n\n", $pee);
    // Space things out a little
    $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
    $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
    $pee = preg_replace('!(' . $allblocks . '>)!', "$1\n\n", $pee);
    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "


\n", $pee); // make paragraphs, including one at the end
    $pee = preg_replace('|


|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    $pee = preg_replace('!

([^<]+)\s*?((?:div|address|form)[^>]*>)!', "


$2", $pee);
    $pee = preg_replace( '|

|', "$1

`` ", $pee ); $pee = preg_replace('!

```\s*(?' . $allblocks . '[^>]>)\s`

!', "$1", $pee); // don't pee all over a tag
    $pee = preg_replace("|


|", "$1", $pee); // problem with nested lists
    $pee = preg_replace('|


> `]*)>|i', "`> ", $pee);

    $pee = str_replace('


', '

', $pee);
    $pee = preg_replace('!
\s*(?' . $allblocks . '[^>]*>)!', "$1", $pee);
    $pee = preg_replace('!(?' . $allblocks . '[^>]*>)\s*

!', "$1", $pee);
    if ($br) {
        $pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", "", "\\0")', $pee);
        $pee = preg_replace('|(?)\s*\n|', "<br></br>\n", $pee); // optionally make line breaks
        $pee = str_replace('', "\n", $pee);
    $pee = preg_replace('!(?' . $allblocks . '[^>]*>)\s*<br></br>!', "$1", $pee);
    $pee = preg_replace('!<br></br>(\s*?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
    if (strpos($pee, '


!is', 'clean_pre', $pee );
    $pee = preg_replace( "|\n$|", '', $pee );

    return $pee;

I use it and I created a helper in my application for it but there is an explication of it.

It's part of Word Press so I'm not sure about the whole thing with the cla but if people are supportive of it I will gladly contact him to see if he has a problem signing one to share the code.

Posted by Joó Ádám (ceriak) on 2008-01-08T14:17:57.000+0000

Thank you for the advice. I'm thinking about the advantages of handling the block level elements, but I don't want to bloat the function. In the meantime, I have improved the regex to avoid generating paragraphs containing only white-space.

Posted by Joó Ádám (ceriak) on 2008-01-08T14:25:24.000+0000

Refactoring a bit: using smarter regex to deal with carriage returns instead of str_replace().

Posted by Jon Whitcraft (sidhighwind) on 2008-01-10T21:56:31.000+0000

I just received an email back from Matt and he said the code is public domain + MIT + GPL so if we go with the more advanced function if we still need Matt to submit a CLA

I think that if you are going to do a paragraph class it should support block level elements and such as if I were to use this I would want to pass in text that I have stored in my db for say a news story or something to this function to normalize the paragraphs which is what I created my helper for.

while I do see your point in a simple function I think the advanced one would fit the 80/20 rule better.

Posted by Joó Ádám (ceriak) on 2008-01-11T12:10:19.000+0000

I've thinked about it, you're right, will be added block-level-awareness. I don't want to copy-paste Matt's code, rather write own, so licensing won't be a problem.

Look back for upgrades in a few days.

Posted by Matthew Weier O'Phinney (matthew) on 2008-02-14T09:25:50.000+0000

On reviewing the comments, I feel that this needs to include block-level awareness before it can be included with ZF. Unless Ádám can complete it by next Wednesday (22 Feb 2008), we'll need to postpone its inclusion until after that release.

Posted by Wil Sinclair (wil) on 2008-03-25T20:33:30.000+0000

Please categorize/fix as needed.

Posted by Ralph Schindler (ralph) on 2008-03-26T11:06:43.000+0000

Hey all, whats the status of this?

Is there code in the incubator for this helper? Is this a Jira based proposal?

Can we get this to Proposal like standards? Tests, docs, etc?


Posted by Joó Ádám (ceriak) on 2008-03-26T17:52:29.000+0000

Ralph: I would like to implement block level awareness as soon as possible, but I was and will be a bit busy in the next ~month because of my work and exams... There's no code in the incubator yet. And yes, something like that :) Should I write a standard proposal for this?

Posted by Matthew Weier O'Phinney (matthew) on 2008-05-09T12:38:50.000+0000

Scheduling for next minor release. Adam, please coordinate with Ralph, and indicate status of block-level awareness.

Posted by Jon Whitcraft (sidhighwind) on 2008-10-28T15:10:42.000+0000

What's the status of this?? Should it be moved to the proposals and done that way?

Posted by Joó Ádám (ceriak) on 2008-10-29T02:45:03.000+0000

I intend to create a proposal for this. I have an almost working code for months now, but there some scurvy bug, which I had no time fix.

Posted by Jon Whitcraft (sidhighwind) on 2008-12-05T06:50:05.000+0000

If you create the proposal and post the code I would be happy to help you find any bugs that exist in the code by writing unittests and such.

Posted by Ralph Schindler (ralph) on 2009-01-10T11:03:21.000+0000

Is there a proposal for this? If not, I am inclined to close this issue.

Posted by Jon Whitcraft (sidhighwind) on 2009-02-06T10:03:02.000+0000

Joó can you post your sample code. I will create the proposal and get this running if you don't have time.

Posted by Joó Ádám (ceriak) on 2009-02-08T03:02:02.000+0000

Jon, I would be glad if you could help me with this. More on this in email. Issue is closed, proposal coming.

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.