Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added autoParagraph functionality

Following previous autoP discussion, this is an update to the function. I haven't done 'test' in cake before, so I will not be able to create the proper test cases.
  • Loading branch information...
commit 6b31c53b61d8d9b39533059f18adf92d26703f1c 1 parent 6f5ff4d
Loki TeckniX authored
Showing with 21 additions and 0 deletions.
  1. +21 −0 lib/Cake/View/Helper/TextHelper.php
21 lib/Cake/View/Helper/TextHelper.php
View
@@ -228,6 +228,27 @@ public function highlight($text, $phrase, $options = array()) {
}
/**
+ * Formats paragraphs around given text for all line breaks
+ * Inspired by WordPress wpautop formatting
+ *
+ * @param string $text Text
+ * @return string The text with proper <p> tags
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoParagraph
+ */
+ public static function autoParagraph($text) {
+ if ( trim($text) !== '' ){
+ $text = preg_replace('|<br />\s*<br />|', "\n\n", $text . "\n"); // just to make things a little easier, pad the end
+ $text = preg_replace("/\n\n+/", "\n\n", str_replace(array("\r\n", "\r"), "\n", $text)); // take care of duplicates and cross-platform newlines
+ $texts = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY); // make paragraphs, including one at the end
+ $text = '';
+ foreach ( $texts as $txt )
+ $text .= '<p>' . trim($txt, "\n") . "</p>\n";
+ $text = preg_replace('|<p>\s*</p>|', '', $text); // Remove empty/whitespace <p> tags
+ }
+ return $text;
+ }
+
+/**
* @see String::stripLinks()
*
* @param string $text Text
Please sign in to comment.
Something went wrong with that request. Please try again.