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.

// $message contains a body of text with non html URLs
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href="\0">\0</a>", $message);

$subject will be the body of text containing flat text urls. $text will now contain our information.

// Output New formatted content with links.
echo $text;

Now our content is parsed with URLs.


Signup for my mailing list

Receive other rambings like this on design, code, and some times food.