note 97972 deleted from function.nl2br by googleguy

From: Date: Thu, 13 Jun 2013 11:07:06 +0000
Subject: note 97972 deleted from function.nl2br by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-195158@lists.php.net to get a copy of this message
Note Submitter: Jose Canciani ---- 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.

« previous php.notes (#195158) next »