note 100120 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:08:42 +0000 |
| Subject: | note 100120 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195168@lists.php.net to get a copy of this message | ||
Note Submitter: dejan dot marjanovic at gmail dot com
----
Hi, I wrote this function to prevent double lines when doing nl2br with pre tag in HTML. Hope it
helps someone.
<?php
function my_nl2br($string){
$string = str_replace("\n", "<br />", $string);
if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
foreach($match as $a){
foreach($a as $b){
$string = str_replace('<pre>'.$b.'</pre>',
"<pre>".str_replace("<br />", "",
$b)."</pre>", $string);
}
}
}
return $string;
}
?>
Source: http://webarto.com/59/php-nl2br-pre