简体   繁体   English

带有BBCode和HTML代码的textarea中的nl2br()

[英]nl2br() in textarea with BBCode and HTML code

I have a textarea where an user can use both BBCode and HTML code in order to insert news in database. 我有一个文本区域,用户可以在其中使用BBCode和HTML代码,以便在数据库中插入新闻。 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. 当我想在网站上显示此新闻时,PHP脚本将BBCode转换为HTML实体,并以保留HTML标记的HTML标准转换新闻内容(从数据库获取)。 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: 问题是当我想通过nl2br()函数将换行符转换为<br />标签时,因为<br />也被添加到HTML标签之后,例如:

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

or 要么

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

Is there a way to avoid this annoying problem? 有没有办法避免这个烦人的问题?

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). 仅当文本之间没有用<p></p>分隔时才有效(我认为这样会更好一些)。

$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. 如前所述,另一种选择是在特定标签内查找文本,并环绕<p>标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM