Turning ASCII Text Into HTML With PHP

Providing a text box for users to type information in is very common, but usually people want to include line breaks and links with the text and they expect the site to lay it out just as they had intended it. The following function will turn any ASCII text string into the approximate HTML equivalent.

function ascii2html($s) {
  $s = htmlentities($s);
  // try and split the text by a double line break
  $paragraphs = split("\n\n",$s);
  if(count($paragraphs) < 2) {
    // if there isn't enough array there then try and split it by single
    $paragraphs = split("\n",$s);		
  };
  for($i = 0,$j = count($paragraphs);$i < $j;$i++) {
    // create links around URLs
    $paragraphs[$i] = preg_replace('/((ht|f)tp:\/\/[^\s&]+)/','<a href="$1">$1</a>',$paragraphs[$i]);
    // create links around email addresses
    $paragraphs[$i] = preg_replace('/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/i','<a href="mailto:$0">$0</a>',$paragraphs[$i]);
    // make paragraph
    $paragraphs[$i] = '<p>'.$paragraphs[$i].'</p>';
  };
  // join all paragraphs and return
  return join("\n",$paragraphs);
}

To test this use the following text sample.

$text = "this is some text
that splits across several
lines and
has some links like this 
one here http://www.hashbangcode.com which
will be used to create a bunch of html";

And call the ascii2html() function like this.

echo ascii2html($text);

This will produce the following output.

<p>this is some text</p>
<p>that splits across several</p>
<p>lines and</p>
<p>has some links like this</p>
<p>one here <a href="http://www.hashbangcode.com">http://www.hashbangcode.com</a> which</p>
<p>will be used to create a bunch of html</p>

It might be prudent to use the strip_tags() function to clean the ASCII text before you use this function as it might lead to invalid HTML.

Comments

Very useful function

 

Thanks

Permalink
Didn't know about the htmlentities() function... This is exactly what I was looking for, thank you. Took me a second to realise that the if-statement was there in order to split paragraphs even if the author had only used single returns, though.
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
15 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.