Skip to content

Instantly share code, notes, and snippets.

@swape
Created May 31, 2013 11:55
Show Gist options
  • Save swape/5684498 to your computer and use it in GitHub Desktop.
Save swape/5684498 to your computer and use it in GitHub Desktop.
It takes url's in a plain text and turn it to html link.
<?php
function formatUrlsInText($strInput){
$strInput = ereg_replace( "www\.", "http://www.", $strInput );
$strInput = ereg_replace( "http://http://www\.", "http://www.", $strInput );
$strInput = ereg_replace( "https://http://www\.", "https://www.", $strInput );
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exUrl, $strInput, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
$strInput = str_replace ($pattern, '<a href=' . $pattern . ' target="_blank">' . $pattern .'</a> ', $strInput);
}
}
return $strInput;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment