Originally posted by batman
$text = wordwrap( $var, 20 );
Wordwrap() will just put new lines (\r\n) in where it feels best (between "words"--this also includes HTML), which will give him the exact same problem.
emrys404, do you by chance have new lines inside the HTML? That's the only time nl2br() should ever put a <br> inside the tag, since it reads the new line there.
You could, theoretically, construct a regular expression that will check whether or not a new line is inside an HTML tag, and then replace it depending on the result. I'm not that great with regular expressions, however, so I can't write it for you.
I hope this helps with the problem. Here's how I would go about it--find all the new lines inside HTML tags via regexs, and then remove them. After they are all removed, run nl2br() on the text.
You'll lose all the new lines inside the HTML, but that shouldn't matter.