Description: Accept a chunk of ASCII text and convert it
into HTML suitable for outputting to a browser.
Useful when you've got text stored in a database and you
want to output it and automatically convert URLs into
real links.
Author: Murray Moffatt for A Web 4 U Designs
History:
2005-02-25 : Initial coding.
2005-08-02 : Make stand-alone and include sample call for
publishing on WeberDev.com.
*/
function ascii2html($text) {
// Encode any HTML tags
$text = htmlentities($text);
// Split the text into an array and treat each element as a paragraph
$textarray = split("\n", $text);
for ($i = 0, $j = count($textarray); $i < $j; $i++) {
// Turn HTTP and FTP URLs into links
$textarray[$i] = preg_replace("/((ht|f)tp:\/\/[^\s&]+)/", "<a href=\"$1\">$1</a>", $textarray[$i]);
// Add paragraphs
$textarray[$i] = "<p>" . $textarray[$i] . "</p>";
}
return join("\n", $textarray);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ASCII To HTML</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="author" content="A Web 4 U Designs - www.aweb4u.co.nz">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>Here is the result:</p>
<?php
$test = <<<TEXTEND
This is the first paragraph.
Bold example: Use the <b> tag to get bold.
Visit http://www.weberdev.com today.
Don't forget to send an email to info@weberdev.com for more info.
TEXTEND;
echo ascii2html($test);
?>
</body>
</html>