PHP中的有个非常好的函数:nl2br(),将文本框中的换行转换为HTML页面的<br />,但是如何实现将html中的<br />换行符转换为文本框中的换行符呢?下面这几个方法将能够帮你解决这个问题。
PHP版将html中的<br />换行符转换为文本框中的换行符:
1 |
function br2nl($text){ |
2 |
return preg_replace('/<br\s*?/??>/i','',$text); |
3 |
} |
或者:
1 |
function br2nl($text){ |
2 |
$text=preg_replace('/<br\s*?/??>/i',chr(13),$text); |
3 |
return preg_replace('/ /i',' ',$text); |
4 |
} |
JS版将html中的<br />换行符转换为文本框中的换行符:
1 |
function br2nl(txt){ |
2 |
var re=/(<br/>|<br>|<BR>|<BR/>)/g; |
3 |
var s=txt.replace(re,"
"); |
4 |
return s; |
5 |
} |