note 62855 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:11:15 +0000 |
| Subject: | note 62855 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195184@lists.php.net to get a copy of this message | ||
Note Submitter: chayden at chayden dot com
----
Using buzoganylaszlo at yahoo dot com simple nl2p function, I've written a more HTML compliant
function to deal with line breaks ...
<?php
/* FORMATS LINE BREAKS WITH PROPER HTML TAGS */
function format_html($content)
{
$content = "<p>" . str_replace("\r\n", "<br/>",
$content) . "";
$content = "" . str_replace("<br/><br/>",
"</p><p>", $content) . "";
return "" . str_replace("<br/><li>", "<li>",
$content) . "";
}
?>
Here is an example of its use ...
<?php
/* CONTENT TO BE PROCCESSED */
$content = "This is a\r\nline break.\r\n\r\nAnd this is a new paragraph.";
/* USE OF format_html FUNCTION */
/* NOTE: If you want 100% proper HTML tags, you'll need to add a closing paragraph tag
(</p>) as shown below. */
echo format_html("$content</p>");
?>
The above will print ...
<p>This is a
<br/>line break.
<p>And this is a paragraph.</p>