Friday, April 25, 2008 - 10:00
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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.
1 2 3 4 5 6 | $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.
1 2 3 4 5 6 | <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.
Category:
Comments
Submitted by Anonymous (not verified) on Thu, 04/21/2011 - 19:25 Permalink
Very useful function
Thanks
Submitted by Koen (not verified) on Thu, 06/07/2012 - 19:36 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.
Add new comment