* Name: paragraph
* Purpose: convert \n and other newline chars to HTML paragraphs
* Note: Uses code from WordPress by Matthew Mullenweg (http://www.photomatt.net; http://www.wordpress.org) * Input:
* - string: input block of text * - br: change single \n to 'br' or not * @param string * @param string * @return string|void */ function smarty_modifier_paragraph($string, $br=true) { if($string != '') { $string = $string . "\n"; // just to make things a little easier, pad the end $string = preg_replace('|
\s*
|', "\n\n", $string); $string = preg_replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1", $string); // Space things out a little $string = preg_replace('!()!', "$1\n", $string); // Space things out a little $string = preg_replace("/(\r\n|\r)/", "\n", $string); // cross-platform newlines $string = preg_replace("/\n\n+/", "\n\n", $string); // take care of duplicates $string = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t

$1

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

\s*?

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

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

]*)>|i', "

", $string); $string = str_replace('

', '

', $string); $string = preg_replace('!

\s*(]*>)!', "$1", $string); $string = preg_replace('!(]*>)\s*

!', "$1", $string); if ($br) $string = preg_replace('|(?)\s*\n|', "
\n", $string); // optionally make line breaks $string = preg_replace('!(]*>)\s*
!', "$1", $string); $string = preg_replace('!
(\s*)!', '$1', $string); $string = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $string); return $string; } else { return; } } /* vim: set expandtab: */