note 89959 deleted from function.nl2br by danbrown
| From: | danbrown@php.net | Date: | Mon, 30 Mar 2009 23:53:14 +0000 |
| Subject: | note 89959 deleted from function.nl2br by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152635@lists.php.net to get a copy of this message | ||
Note Submitter: Rashaud Teague maris at marislabs dot org
----
@ sean at boyercentral dot net
I have re-written your code to make it work better:
<?php
function nl2brPre($string, $is_xhtml = true) {
$str = explode("\n", $string);
$op = '';
for ($i = 0; $i < sizeof($str); $i++) {
if (preg_match("/\<pre/i", $str[$i])) {
$op .= trim($str[$i])."\n";
$n = $i+1;
for ($j = $n; $j < sizeof($str); $j++, $i++) {
if (preg_match("/\<\/pre>/i", $str[$j])) {
$op .= trim($str[$j]);//(($is_xhtml == true) ? '<br />' :
'<br>');
break;
}
$op .= htmlspecialchars(trim($str[$j]))."\n";
}
} else {
$op .= trim($str[$i]).(($is_xhtml == true) ? '<br />' : '<br>');
}
}
return $op;
}
?>