Home /php /[PHP] find links from plain text and link them /

[PHP] find links from plain text and link them

2:17 am, April 23, 2017

Original Post Date: 1:21 am, October 15, 2014

// find links from plain text and link them. function find_links($text) { $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; // The Regular Expression filter if(preg_match($reg_exUrl, $text, $url)) { // Check if there is a url in the text $url[0] = rtrim($url[0], ','); // remove trailing comma if there is one. $text = preg_replace($reg_exUrl, "{$url[0]} ", $text); // make the urls hyper links return $text; } else { return $text; // if no urls in the text just return the text } } // usage $text = "text string with a normal url http://google.com in it"; $text = find_links($text); echo $text;