-
Find link to the string
This function searches a string for urls and converts them into anchor tags. This is particularly useful for tweets pulled from Twitter using the API. This is used to prepare the links in any Twitter feed at the bottom of the website.
View Code PHP1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
function make_links($str) { $reg_ex_url = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; $urls = array(); $urls_to_replace = array(); if (preg_match_all($reg_ex_url, $str, $urls)) { $numOfMatches = count($urls[0]); $url_count = 0; for($i=0; $i<$numOfMatches; $i++) { $alreadyAdded = false; $url_count = count($urls_to_replace); for($j=0; $j<$url_count; $j++) { if($urls_to_replace[$j] == $urls[0][$i]) { $alreadyAdded = true; } } if(!$alreadyAdded) { array_push($urls_to_replace, $urls[0][$i]); } } $url_count = count($urls_to_replace); for($i=0; $i<$url_count; $i++) { $str = str_replace($urls_to_replace[$i], "".$urls_to_replace[$i]." ", $str); } return $str; } else { return $str; } }
Tags: search string for url