note 50816 added to function.nl2br
| From: | mike at openconcept dot ca | Date: | Thu, 10 Mar 2005 18:02:03 +0000 |
| Subject: | note 50816 added to function.nl2br | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86214@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.nl2br.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50816
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50816&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50816&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50816&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50816&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50816&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50816&report=yes
Search -- http://master.php.net/manage/user-notes.php