We just released some code to make our software a little bit more user friendly, and it backfired. Basically, we’re attempting to replace newlines with <br /> tags. The trouble is, sometimes our users will enter code like the following:
<a
href='http://nowhere.com'>Nowhere</a>
When we run our code, this translates to
<a <br />href='http://nowhere.com' />Nowhere</a>
which obviously doesn’t render properly.
Is there a regular expression or a PHP function to strip, or perhaps compress, the whitespace between the attributes of an HTML tag?
Clarification: This isn’t full HTML. It’s more similar to Markdown or some other language (we will eventually be moving to Markdown, but I need a quick fix). So I can’t just parse this as regular HTML. The newlines need to be converted to <br /> tags properly.
After some searching and much trial and error, I have come up with the following solution/hack:
After executing this code, all HTML tags in
$textwill be properly formatted and valid with NO newline characters.I know that this isn’t the best solution, but it works, and pretty soon we’ll be migrating to a true markup language (such as Markdown).