Created
May 31, 2013 11:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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