note 50816 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:07:54 +0000 |
| Subject: | note 50816 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195164@lists.php.net to get a copy of this message | ||
Note Submitter: mike at openconcept dot ca
----
There are other nl2p examples above, but think this one will provide nicer html. Also threw in a
very related br2p function for all of those folks who want to strip away the <br /> tags which
give their designers the blues.
/**
* replacement for php's nl2br tag that produces more designer friendly html
*
* Modified from: http://www.php-editors.com/contest/1/51-read.html
*
* @param string $text
* @param string $cssClass
* @return string
*/
function nl2p($text, $cssClass=''){
// Return if there are no line breaks.
if (!strstr($text, "\n")) {
return $text;
}
// Add Optional css class
if (!empty($cssClass)) {
$cssClass = ' class="' . $cssClass . '" ';
}
// put all text into <p> tags
$text = '<p' . $cssClass . '>' . $text . '</p>';
// replace all newline characters with paragraph
// ending and starting tags
$text = str_replace("\n", "</p>\n<p" . $cssClass .
'>', $text);
// remove empty paragraph tags & any cariage return characters
$text = str_replace(array('<p' . $cssClass . '></p>',
'<p></p>', "\r"), '', $text);
return $text;
} // end nl2p
/**
* expanding on the nl2p tag above to convert user contributed
* <br />'s to <p>'s so it displays more nicely.
*
* @param string $text
* @param string $cssClass
* @return string
*/
function br2p($text, $cssClass=''){
if (!eregi('<br', $text)) {
return $text;
}
if (!empty($cssClass)) {
$cssClass = ' class="' . $cssClass . '" ';
}
// put all text into <p> tags
$text = '<p' . $cssClass . '>' . $text . '</p>';
// replace all break tags with paragraph
// ending and starting tags
$text = str_replace(array('<br>', '<br />',
'<BR>', '<BR />'), "</p>\n<p" . $cssClass .
'>', $text);
// remove empty paragraph tags
$text = str_replace(array('<p' . $cssClass . '></p>',
'<p></p>', "<p>\n</p>"), '', $text);
return $text;
}
This is all code from Back-End CMS (http://www.back-end.org), a template based gpl php/mysql cms.
Mike