ZF-2348: Zend_View_Helper_Paragraph
Description
A new view helper, intended to replace nl2br() - ease and propagate the use of the semantically meaningful
<
p> element instead of
.
Details:
- 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.
Code:
class Zend_View_Helper_Paragraph
{
public function paragraph($text)
{
return '' . preg_replace('/[\r\n]+(\h+[\r\n]+)?/', '
', trim($text)) . '
';
}
}
Usage:
In your view:
echo $this->paragraph($this->text);
Comments
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?
$1\n", $pee); // make paragraphs, including one at the end $pee = preg_replace('|\s*?|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace $pee = preg_replace('!([^<]+)\s*?((?:div|address|form)[^>]*>)!', "$1$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('|', $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|', "\n", $pee); // optionally make line breaks $pee = str_replace('', "\n", $pee); } $pee = preg_replace('!(?' . $allblocks . '[^>]*>)\s*
!', "$1", $pee); $pee = preg_replace('!
(\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. http://photomatt.net/scripts/autop/
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?
-ralph
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.