note 97972 added to function.nl2br
| From: | JoseCanciani at osu1 dot php dot net | Date: | Wed, 19 May 2010 03:57:33 +0000 |
| Subject: | note 97972 added to function.nl2br | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169386@lists.php.net to get a copy of this message | ||
Recursive solution to avoid replacing newlines inside <pre> tags:
function findOpenPre($line) {
$start = strpos($line,'<pre');
if ($start === false) {
return nl2br($line);
} else {
return nl2br(substr($line,0,$start-1)).findClosePre(substr($line,$start));
}
}
function findClosePre($line) {
$start = strpos($line,'</pre>');
if ($start === false) {
return $line.'</pre>'; # should never come here
} else {
return substr($line,0,$start+6).findOpenPre(substr($line,$start+7));
}
}
echo findOpenPre('hello '.chr(10).'<pre>
world'.chr(10).'</pre>');
will return "hello <br><pre> world\n</pre>"
instead of "hello <br><pre> world<br></pre>"
Jose.
----
Server IP: 69.147.83.197
Probable Submitter: 190.245.24.207
----
Manual Page -- http://www.php.net/manual/en/function.nl2br.php
Edit -- https://master.php.net/note/edit/97972
Del: integrated -- https://master.php.net/note/delete/97972/integrated
Del: useless -- https://master.php.net/note/delete/97972/useless
Del: bad code -- https://master.php.net/note/delete/97972/bad+code
Del: spam -- https://master.php.net/note/delete/97972/spam
Del: non-english -- https://master.php.net/note/delete/97972/non-english
Del: in docs -- https://master.php.net/note/delete/97972/in+docs
Del: other reasons-- https://master.php.net/note/delete/97972
Reject -- https://master.php.net/note/reject/97972
Search -- https://master.php.net/manage/user-notes.php