Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top