Regular Expression for URLs using ereg_replace
Ever needed a Regular Expression to parse URLs in a body of text. The beauty with this expression is its flexibility, it will handle a http or https url with or without www and it’s fine with tinyurls.
[sourcecode language="php"]
// $message contains a body of text with non html URLs
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $message);
[/sourcecode]
$subject will be the body of text containing flat text urls. $text will now contain our information.
[sourcecode language="php"]
// Output New formatted content with links.
echo $text;
[/sourcecode]
Now our content is parsed with URLs.
Done!