nl2br() puts break inside tag
Posted: Sun Jan 31, 2010 11:29 pm
I am a novice at PHP but an experienced programmer. I am extracting some data from a DB and displaying it on the webpage. I am using nl2br() to ensure the formatting looks appropriate. Unfortunately, there is at least one passage to display that has an HTML link in it, with a newline embedded in a link, thusly:
...lots of interesting text yadda yadda yadda <a href=
"http://the.links.url">click me</a>and yet more stuff....
nl2br() happily replaces the newline in the link with a <br> thus messing up not only the link itself but the subsequent text display. If I don't use nl2br(), then all the text in all passages are displayed as a single block of text.
Short of writing a function to parse the text and determine if the newline is within a tag, any other suggestions for how to make the formatting good but not mess up the tag? I was surprised there wasn't an option in nl2br() to skip tags, or some other function which does a similar thing. Did I miss it?
Updating the DB to remove the newline is possible but I'd have to search all for such instances, then prevent such future transgressions. Right now I don't have control over what goes in.
...lots of interesting text yadda yadda yadda <a href=
"http://the.links.url">click me</a>and yet more stuff....
nl2br() happily replaces the newline in the link with a <br> thus messing up not only the link itself but the subsequent text display. If I don't use nl2br(), then all the text in all passages are displayed as a single block of text.
Short of writing a function to parse the text and determine if the newline is within a tag, any other suggestions for how to make the formatting good but not mess up the tag? I was surprised there wasn't an option in nl2br() to skip tags, or some other function which does a similar thing. Did I miss it?
Updating the DB to remove the newline is possible but I'd have to search all for such instances, then prevent such future transgressions. Right now I don't have control over what goes in.