LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP, nl2br except between HTML tags? (https://www.linuxquestions.org/questions/programming-9/php-nl2br-except-between-html-tags-4175470505/)

audiodef 07-22-2013 09:55 AM

PHP, nl2br except between HTML tags?
 
How would I write a PHP function to use nl2br except between any two HTML tags? For example, the following:

Code:

Blah, blah, blah. Here is some text.

And now here is some more text.

<table>
<tr>
<td>
Followed by stuff in a table.
</td>
</tr>
</table>

I don't want the table exploded into extra line breaks by nl2br because the text within it won't appear spaced correctly. However, there should be new lines between the first two lines of text as it appears above. I want this all handled by one function because this will all be one string retrieved from database tables. Of course, I'm open to any exploding of the original string to facilitate this.

Snark1994 07-22-2013 11:42 AM

I can't think of any good (emphasis on that 'good') way to do this except through using a full-blown HTML parser. Perhaps it may be easier to just amend the database tables so they have the <br/> in them already?


All times are GMT -5. The time now is 02:16 AM.