From d260f4a5b3c4e057355397da65ab7f8a3088d5a7 Mon Sep 17 00:00:00 2001
From: Loki
test text
+ +TEXT; + $result = $this->Text->autoParagraph($text); + $this->assertEquals($expected, $result); + $result = $this->Text->autoParagraph($text); + $text = 'This is atest text
+ +TEXT; + $result = $this->Text->autoParagraph($text); + $this->assertEquals($expected, $result); + $text = <<This is a new line.
+ +TEXT; + $result = $this->Text->autoParagraph($text); + $this->assertEquals($expected, $result); + } + } diff --git a/lib/Cake/View/Helper/TextHelper.php b/lib/Cake/View/Helper/TextHelper.php index 4951583b0..d5df9288f 100644 --- a/lib/Cake/View/Helper/TextHelper.php +++ b/lib/Cake/View/Helper/TextHelper.php @@ -228,6 +228,29 @@ class TextHelper extends AppHelper { return $this->_engine->highlight($text, $phrase, $options); } +/** + * Formats paragraphs around given text for all line breaks + *added for double line return + * + * @param string $text Text + * @return string The text with proper
and
tags
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoParagraph
+ */
+ public function autoParagraph($text) {
+ if (trim($text) !== '') {
+ $text = preg_replace('|
]*>\s*
]*>|i', "\n\n", $text . "\n");
+ $text = preg_replace("/\n\n+/", "\n\n", str_replace(array("\r\n", "\r"), "\n", $text));
+ $texts = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY);
+ $text = '';
+ foreach ($texts as $txt) {
+ $text .= '
' . nl2br(trim($txt, "\n")) . "
\n"; + } + $text = preg_replace('|\s*
|', '', $text); + } + return $text; + } + /** * @see String::stripLinks() *