Plain Text To HTML Converter
When people type things into your form, especially when it is a textarea field, you may want to automatically give it HTML markup. This article provides a function to let you do that.
Your reason for converting text received from a form into HTML markup may be one of the following, or something else entirely.
-
The information is to be published immediately on a web page, perhaps a comment to an article.
-
The information is stored in a database but may be used in an email or on a web page later on.
-
You build your own text to HTML converter for converting text you paste into a form field. It may be a private converter or something built for the public to use.
The plain text to HTML conversion function converts sequential text line breaks (2 or more) into paragraph separations. No matter how many line breaks in a row, there is only one paragraph separation. The <p>
and </p>
tags are used for sequential line breaks.
Single line breaks are converted into <br >
characters. This allows one-line items to be provided without paragraph separation. Examples are recipe ingredients or poems.
Unless it is told not to, the conversion function will replace URLs to images with an image tag to display that image. Only images that a browser can display are converted that way.
Also unless it is told not to, the function will convert URLs into clickable link tags.
And also (this is an important option) unless it is told not to, the function will replace bracket characters with their HTML code equivalent. The reason you might want to let the function go ahead and do this is to disable JavaScript injection and other attacks that may be tried with form data input.
Here is the source code for the ConvertPlainTextToHTML()
function. It is copy and paste with no customizations needed. Instructions for use follow the source code.
function ConvertPlainTextToHTML( $text, $url2img=true, $url2link=true, $brackets2codes=true ) { // Reducing injection attack possibiliites by converting bracket characgers into HTML entities. if($brackets2codes) { $text = str_replace('<','<',$text); $text = str_replace('>','>',$text); } // Convert text for P and BR tags. $text = trim($text); $text = strpos($text,"\n")===false ? str_replace("\r","\n",$text) : str_replace("\r",'',$text); $text = preg_replace('/\n\n+/','</p><p>',$text); // Convert sequential linefeeds to HTML paragraph breaks. $text = str_replace("\n","\n<br />",$text); // Convers single linefeeds to a single HTML linebreak. $text = preg_replace('!</p><p>!',"\n</p>\n<p>\n",$text); // Make source code more human readable. $text = "<p>\n$text\n</p>"; // Wrap text in paragraph tags. // Image URLs converted into an IMG tag. if($url2img) { $matches = array(); preg_match_all('!\b(https?://[a-zA-Z0-9\-\._\~\:/\'\+\=\%\(\)]+\.(png|jpg|jpeg|gif|webp|avif))!s',$text,$matches); foreach( $matches[1] as $match ) { $encoded = quotemeta($match); $text = preg_replace( '!\b'.$encoded.'!s', '<img src="h__HH__h' . $match . '" style="max-width:100%;" />', $text ); } } // Non-image URLs converted into an A tag. if($url2link) { $matches = array(); preg_match_all('!\b(https?://[a-zA-Z0-9\-\._\~\:/\?\#\[\]\@\!\$\&\'\(\)\*\+,;\=\%]+)!s',$text,$matches); foreach( $matches[1] as $match ) { $encoded = quotemeta($match); $text = preg_replace( '!\b'.$encoded.'!s', '<a href="' . $match . '">' . $match . '</a>', $text ); } } // Replace URLs in IMG tags from "h__HH__hhttp://" to "http://" and from "h__HH__hhttps://" to "https://" // that were placed during image URL to IMG tag conversion. $text = str_replace('h__HH__hhttp','http',$text); // Return the conversion. return $text; } # function ConvertPlainTextToHTML()
Put the above function into the PHP script that your form submits to, which would be your form-handling script. Pretty much anywhere within the PHP script should work so long as it is not within another function. If uncertain, put it near the top, perhaps immediately below the <?php
line that signifies the beginning of the PHP code.
Within your form-handling script, call the ConvertPlainTextToHTML()
function with the text you want to convert. See the example below for how to do that.
As an example, let's suppose the variable $_POST['text']
contains the text provided by a value="text"
form field (probably a textarea
field, which accepts multi-line text).
To convert the content of $_POST['text']
into HTML, do this within your PHP form-handling script:
$_POST['text'] = ConvertPlainTextToHTML( $_POST['text'] );
$_POST['text']
now contains the HTML coded text.
The ConvertPlainTextToHTML()
function automatically converts absolute image URLs into image tags, other absolute URLs into clickable links, and bracket characters into HTML-entity codes. (Absolute URLs begin with http://
or https://
.)
If that is the way you want it, then the above example is the one you go by. However, if you want to turn any of those off, you'll need to go by the following example of the function call.
To explain, the ConvertPlainTextToHTML()
function will expect data in a certain order:
- text to convert
- convert image URLs?
- convert other URLs?
- convert brackets?
The "?" items are specified either true
or false
. Here is an example.
$_POST['text'] = ConvertPlainTextToHTML( $_POST['text'], true, true, false )
The above code tells ConvertPlainTextToHTML()
to not convert bracket characters.
Now you have two ways to call the ConvertPlainTextToHTML
function, convert text with everything default or convert text by telling the function not to do one or more supporting conversions: image URL to image tag, URL to clickable link, and/or bracket character conversion.
To reiterate, here are the two methods.
A: To run the default version, use:
$_POST['text'] = ConvertPlainTextToHTML( $_POST['text'] );B: To suppress one or more supporting conversions (specify true and/or false at each of the last 3 positions) use:
$_POST['text'] = ConvertPlainTextToHTML( $_POST['text'], true, true, false )
For completion, here is an example form that is also the form processing script — the form submits to itself. When text is provided in the form, the response will be the text converted so it has HTML markup.
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Text to HTML Conversion Example</title> <style type="text/css"> @charset "utf-8"; * { box-sizing:border-box; } html { font-family:sans-serif; font-size:100%; } </style> </head> <body> <div style="max-width:500px; margin:.5in auto;"> <h1>Text to HTML Conversion Example</h1> <?php if( isset($_POST['text']) ): ?> <h2>Converted Text</h2> <?php /* Start of example script. */ function ConvertPlainTextToHTML( $text, $url2img=true, $url2link=true, $brackets2codes=true ) { // Reducing injection attack possibiliites by converting bracket characgers into HTML entities. if($brackets2codes) { $text = str_replace('<','<',$text); $text = str_replace('>','>',$text); } // Convert text for P and BR tags. $text = trim($text); $text = strpos($text,"\n")===false ? str_replace("\r","\n",$text) : str_replace("\r",'',$text); $text = preg_replace('/\n\n+/','</p><p>',$text); // Convert sequential linefeeds to HTML paragraph breaks. $text = str_replace("\n","\n<br />",$text); // Convers single linefeeds to a single HTML linebreak. $text = preg_replace('!</p><p>!',"\n</p>\n<p>\n",$text); // Make source code more human readable. $text = "<p>\n$text\n</p>"; // Wrap text in paragraph tags. // Image URLs converted into an IMG tag. if($url2img) { $matches = array(); preg_match_all('!\b(https?://[a-zA-Z0-9\-\._\~\:/\'\+\=\%\(\)]+\.(png|jpg|jpeg|gif|webp|avif))!s',$text,$matches); foreach( $matches[1] as $match ) { $encoded = quotemeta($match); $text = preg_replace( '!\b'.$encoded.'!s', '<img src="h__HH__h' . $match . '" style="max-width:100%;" />', $text ); } } // Non-image URLs converted into an A tag. if($url2link) { $matches = array(); preg_match_all('!\b(https?://[a-zA-Z0-9\-\._\~\:/\?\#\[\]\@\!\$\&\'\(\)\*\+,;\=\%]+)!s',$text,$matches); foreach( $matches[1] as $match ) { $encoded = quotemeta($match); $text = preg_replace( '!\b'.$encoded.'!s', '<a href="' . $match . '">' . $match . '</a>', $text ); } } // Replace URLs in IMG tags from "h__HH__hhttp://" to "http://" and from "h__HH__hhttps://" to "https://" // that were placed during image URL to IMG tag conversion. $text = str_replace('h__HH__hhttp','http',$text); // Return the conversion. return $text; } # function ConvertPlainTextToHTML() $_POST['text'] = ConvertPlainTextToHTML( $_POST['text'] ); echo '<div style="white-space:pre-wrap; margin-bottom:1em; padding:.3em; outline:3px solid #ccc;">'; echo htmlspecialchars($_POST['text']); echo '</div>'; /* End of example script. */ ?> <p> The above is the converted text. To convert more, use the form again. </p> <?php endif; ?> <h2>The Form</h2> <form action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post"> <p>Type or paste text here:<br> <textarea name="text" style="width:100%; height:1.5in;"></textarea> </p> <p> <input type="submit" value="submit" style="width:100%;"> </p> </form> </div> </body> </html>
Save the above as temp.php
(or other *.php
file name) and upload it to your server. To use temp.php
, type its URL into your browser.
The ConvertPlainTextToHTML()
function is ready to paste into your PHP scripts. One simple function call and you have basic HTML formatted text.
(This content first appeared in Possibilities newsletter.)
Will Bontrager