【PHP】nl2br( )
1.nl2br( ) — 在字符串所有新行之前插入 HTML 换行标记
string nl2br ( string $string
[, bool $is_xhtml
= true ] ) 在字符串 string
所有新行之前插入 '<br />'
或 '<br>'
,并返回。
<?php $string = "This\r\nis\n\ra\nstring\r"; echo nl2br($string); ?>
输出html结果:
This<br /> is<br /> a<br /> string<br />
2.br2nl( )——将html中的<br />换行符转换为文本框中的换行符
php代码:
function br2nl($text){ return preg_replace('/<br\\s*?\/??>/i','',$text); }
function br2nl($text){ $text=preg_replace('/<br\\s*?\/??>/i',chr(13),$text); return preg_replace('/ /i',' ',$text); }
js代码:
function br2nl(txt){ var re=/(<br\/>|<br>|<BR>|<BR\/>)/g; var s=txt.replace(re,"\n"); return s; }