[Solar-svn] Revision 3159

pmjones at solarphp.com pmjones at solarphp.com
Thu May 15 14:25:25 CDT 2008


Solar_View_Helper_Nl2p: [NEW] Helper to convert 2 or more newlines in text to paragraph tags.


Added: trunk/Solar/View/Helper/Nl2p.php
===================================================================
--- trunk/Solar/View/Helper/Nl2p.php	                        (rev 0)
+++ trunk/Solar/View/Helper/Nl2p.php	2008-05-15 19:25:23 UTC (rev 3159)
@@ -0,0 +1,52 @@
+<?php
+/**
+ * 
+ * Converts 2 or more newlines to paragraph tags.
+ * 
+ * @category Solar
+ * 
+ * @package Solar_View
+ * 
+ * @author Paul M. Jones <pmjones at solarphp.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version $Id$
+ * 
+ */
+class Solar_View_Helper_Nl2p extends Solar_View_Helper
+{
+    /**
+     * 
+     * Converts 2 or more newlines to paragraph tags.
+     * 
+     * @param string $text The text on which to change newlines into <p> tags.
+     * 
+     * @param array $attr Attributes for each <p> tag.
+     * 
+     * @return string The escaped text, with newlines changed to paragraphs.
+     * 
+     */
+    public function nl2p($text, $attr = array())
+    {
+        // normalize line endings
+        $text = str_replace(array("\r\n", "\r"), "\n", $text);
+        
+        // arribs for <p> tags
+        $attribs = $this->_view->attribs($attr);
+        
+        // split on 2 newlines, with any space between them as well
+        $grafs = preg_split('/\n\s*\n/', $text);
+        
+        // loop through the paragraph blocks
+        $html = '';
+        foreach ($grafs as $graf) {
+            $graf = trim($graf);
+            if ($graf) {
+                $html .= "<p$attribs>" . $this->_view->escape($graf) . "</p>\n\n";
+            }
+        }
+        
+        return $html;
+    }
+}




More information about the Solar-svn mailing list