<?php
$hostRegex = "([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-a-z\d]*[a-z]";
$portRegex = "(:\d{1,})?";
$pathRegex = "(\/[^\s?]+)?";
$queryRegex = "(\?[^<>#\"\s]+)?";
$urlRegex = "/(?:(?<=^)|(?<=\s))((ht|f)tps?:\/\/" . $hostRegex . $portRegex . $pathRegex . $queryRegex . ")/";
$str = "This is my homepage: http://home.example.com.";
$str2 = "This is my homepage: http://home.example.com:8181/index.php";
$sample1 = preg_replace($urlRegex, "<a href=\"\\1\">\\1</a>", $str);
$sample2 = preg_replace($urlRegex, "<a href=\"\\1\">\\1</a>", $str2);
echo $sample1 . "\n";
echo $sample2 . "\n";
?>
|