nl2br() in textarea with BBCode and HTML code

元气小坏坏 提交于 2019-12-25 14:32:14

问题


I have a textarea where an user can use both BBCode and HTML code in order to insert news in database. When I want to show this news in site, PHP script convert BBCode to HTML entities and convert news content (get from database) in HTML standard preserving HTML tags. The problem is when I want to convert newline to <br /> tag with nl2br() function, because <br /> is added after HTML tags too, for example:

<li>Some text</li><br />

or

<table><br />
<tr><br />
etc.

Is there a way to avoid this annoying problem?


回答1:


I am thinking about a regex. This should only work if texts are not separated by <p></p> (which in my opinion would be a little better).

$formattedText = preg_replace("/(<[a-zA-Z0-9=\"\/\ ]+>)<br\ \/>/", "$1", nl2br($text));

As mentioned, the other option would be to look for text inside specific tags, and wrap around <p> tags.



来源:https://stackoverflow.com/questions/13092418/nl2br-in-textarea-with-bbcode-and-html-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!