Return to Snippet

Revision: 60895
at November 16, 2012 19:58 by halk


Initial Code
/**
 * AutoLinkUrls()
 * 
 * @param mixed $str
 * @param bool $popup
 * @return void
 */
function AutoLinkUrls($str,$popup = FALSE){
    if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)){
		$pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
		for ($i = 0; $i < count($matches['0']); $i++){
			$period = '';
			if (preg_match("|\.$|", $matches['6'][$i])){
				$period = '.';
				$matches['6'][$i] = substr($matches['6'][$i], 0, -1);
			}
			$str = str_replace($matches['0'][$i],
					$matches['1'][$i].'<a href="http'.
					$matches['4'][$i].'://'.
					$matches['5'][$i].
					$matches['6'][$i].'"'.$pop.'>http'.
					$matches['4'][$i].'://'.
					$matches['5'][$i].
					$matches['6'][$i].'</a>'.
					$period, $str);
		}//end for
    }//end if
    return $str;
}//end AutoLinkUrls

Initial URL


Initial Description
Does not require http in the text to link.  It must start with www however.  Optionaly make the links popup in a new window.

Initial Title
Automatically turn all text urls into working Hyperlinks

Initial Tags
php

Initial Language
PHP