Turn Plain Text URLs into Active Links using PHP
by Bandicoot Marketing on | Posted in Tutorials | 10 comments
I was putting together a bit of code to pull in a Twitter feed, similar to what you see in the footer of bavotasan.com. I got everything working properly but the only problem was all of the URLs were just plain text instead of active links. I figured out a way to automatically turn the URLs into active links using a small piece of PHP.
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);
The code finds all instances of http://
and converts the string into an active link by surrounding it with an anchor tag.
10 comments for “Turn Plain Text URLs into Active Links using PHP”