note 12478 deleted from function.nl2br by aidan

From: Date: Sun, 10 Oct 2004 10:39:54 +0000
Subject: note 12478 deleted from function.nl2br by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-78290@lists.php.net to get a copy of this message
Note Submitter: 7stud@devshed ---- nl2br() FOR PARAGRAPHS(windows): Note: The notation <"p> for a paragraph tag is necessary for proper display. For text entered in a textfield, I wanted to change newlines to <br /> tags and paragraphs to <"p> tags. Since, newlines are \n and paragraphs are \n\r I thought I would try this: $new_text=str_replace("\n\r\", "<"p>", $text); $new_text=nl2br($new_text); On the first pass, this code replaces all the \n\r sequences with <"p> tags, and on the second pass, it replaces the remaining newlines(\n) with <br /> tags. But, for some reason \n\r sequences end up getting replaced by a <"p> tag AND a <br /> tag causing two lines to be skipped at the start of a new paragraph. In the course of this, I discovered that nl2br() will replace \n\r with two <br /> tags, thereby preserving the original format. If for some reason you require <"p> tags for paragraphs, I found that adding an extra \n works: $new_name=str_replace("\n\r\n", "<"p>", $name); $new_name=nl2br($new_name); Note: the resulting code will not be XHTML 1.0 compliant since the <"p> tags are not matched by closing <"/p> tags.

« previous php.notes (#78290) next »