#42: Converting Plaintext into HTML-Ready Markup

One annoying web aspect of programming is that HTML and plaintext aren't compatible, yet they are frequently used interchangeably. People type things into text areas of forms in plaintext, but chances are pretty good you want to display them in HTML.

Here's an example of something that you might find on a form:

Hey folks,
I recently had my brain removed as a part of my therapy. I am fine, and I can now enjoy Pauly Shore movies with my friends.
Sincerely,
Fred

Because there are no <BR /> or <P> tags in the markup for line breaks, it looks like this when pumped back into a web browser as HTML:

Hey folks, I recently had my brain removed as a part of my therapy. I am fine, and I can now enjoy Pauly Shore movies ...

Get Wicked Cool PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.