I use nl2br
当显示保存在某处的一些信息时,但是当使用 HTML 标签时我不想添加<br>
他们的标签。
例如,如果我使用
<table>
<th></th>
</table>
它将被转换为
<table><br />
<th></th><br />
</table><br />
这为这张桌子腾出了很多空间。
为何只能为其他非 HTML 内容添加换行标签?
Thanks.
我也有同样的问题,
我编写了这段代码,添加了<br />
在每行的末尾,除非该行以 html 标记结束:
function nl2br_save_html($string)
{
if(! preg_match("#</.*>#", $string)) // avoid looping if no tags in the string.
return nl2br($string);
$string = str_replace(array("\r\n", "\r", "\n"), "\n", $string);
$lines=explode("\n", $string);
$output='';
foreach($lines as $line)
{
$line = rtrim($line);
if(! preg_match("#</?[^/<>]*>$#", $line)) // See if the line finished with has an html opening or closing tag
$line .= '<br />';
$output .= $line . "\n";
}
return $output;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)