Recursive solution to avoid replacing newlines inside <pre> tags:
<?php
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.
--was--
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.
http://php.net/manual/en/function.nl2br.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.