Return to Snippet

Revision: 27932
at June 27, 2010 06:17 by ptejada


Initial Code
function parseURLs($str, $length = 15){
		preg_match_all('@(https?://([-\w\.]+)+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)@',$str,$matches);
		if($matches){
			foreach($matches[0] as $url){
				$text = $length ? shortText($url,$length) : $url;
				$replace = '<a href="'.$url.'">'.$text.'</a>';
				$str = str_replace($url,$replace,$str);
			}
		}		
		return $str;			
	}
	
	function shortText($text, $chars){
		$str = $text;
		if(strlen($str) > $chars){ 
			$str = substr($str, 0, $chars); 
			$str .= "..."; 
			return $str;
		}else { 
			return $str; 
		} 
	}

Initial URL

                                

Initial Description

                                

Initial Title
URL to HTML link PHP parser

Initial Tags
url, php, link, html

Initial Language
PHP