PHP Find URLs in Text And Make Hyper Link
The basic function of this is to find any URLs in the block of text and turn them into hyperlinks.
It will only find URLs if they are properly formatted, meaning they have a www, http, https, ftp or ftps.
function textTohyperlink($text) { // The Regular Expression filter $reg_exUrl = "/((((http|https|ftp|ftps)\:\/\/)|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(\/\S*)?)/"; // Check if there is a url in the text if(preg_match_all($reg_exUrl, $text, $url)) { // make the urls hyper links $matches = array_unique($url[0]); foreach($matches as $match) { $replacement = "<a href=".$match." target='_blank' rel='nofollow'>{$match}</a>"; $text = str_replace($match,$replacement,$text); } return nl2br($text); } else { // if no urls in the text just return the text return nl2br($text); } } $text = "The text you want to filter goes here. http://hkpatel201.blogspot.com"; echo textTohyperlink($text);
Please support us, Like us on Facebook.
Subscribe to:
Post Comments (Atom)
Patel! You are a star, this is what i am looking to amend on my site http://www.indiagrowing.com
ReplyDeleteThanks so much, i will try and let you know.