note 97972 modified in function.nl2br by danbrown

From: Date: Wed, 19 May 2010 12:20:21 +0000
Subject: note 97972 modified in function.nl2br by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169408@lists.php.net to get a copy of this message
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

« previous php.notes (#169408) next »