scriptingBlog.com - For those who work online
Do you have something interesting to share with others? If you do, click here to write a post or a press-release to the blog. All 100% free!

Text to HTML conversion

PHP function for plain text to HTML conversion.

Source code:


<?
function txt2html($txt){
    $txt preg_replace("/([a-z0-9\._-]+)@([a-z0-9\.-]+)\.([a-z]{2,3})/i""<a href='mailto:$1@$2.$3'>$1@$2.$3</a>"$txt);
    $txt preg_replace("/(\s)http:\/\/(.+)(\s)/i""$1<a href='http://$2'>http://$2</a>$3"$txt);
    $txt preg_replace("/(\s)www\.(.+)(\s)/i""$1<a href='http://www.$2'>www.$2</a>$3"$txt);
    $txt nl2br($txt);
    $txt preg_replace("/\s{2}/"" &nbsp;"$txt);
    $txt "<div style='font-family: \"Courier New\", Courier, monospace; font-size: 12px;'>{$txt}</div>";
    return $txt;
}
?>

Leave a Reply