Check whether a string contains a URL
Posted: Tue Oct 18, 2011 6:44 am
I'm trying to set this up so that if regex matches a URL in the string of text, it will turn that URL into an active link.
The way my current code is set up, it doesn't work. How do I make PHP detect whether there is a URL present in a chunk of text?
Code: Select all
<?php
$text = "What's up man this is the link to the page: http://google.com";
if (ereg('^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$',$text)) {
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);
}
echo "$text";
?>