Created
October 17, 2011 03:24
-
-
Save stephenyeargin/1291871 to your computer and use it in GitHub Desktop.
Hyperlink filtering from a body of text (with hack to catch t.co links because I don't like RegEx at all).
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
/** | |
* Auto convert hyperlinks in text | |
* | |
* @param string Text to filter | |
* @return string Filtered text | |
* @url http://www.php.net/manual/en/function.preg-replace.php#77787 | |
*/ | |
function filter_hyperlink($text) { | |
$text=preg_replace("/(http:\/\/|www|[a-zA-Z0-9-]+\.|[a-zA-Z0-9\.-]+@)(([a-zA-Z0-9-][a-zA-Z0-9-]+\.)+[a-zA-Z0-9-\.\/\_\?\%\#\&\=\;\~\!\(\)]+)/","<a href=\"http://\\1\\2\">\\1\\2</a>",$text); | |
$text=preg_replace("/(http:\/\/t\.co\/[a-zA-Z0-9-\.\/\_\?\%\#\&\=\;\~\!\(\)]+)/","<a href=\"http://\\1\\2\">\\1\\2</a>",$text); // Hack for twitter t.co links | |
// Remove double protocols | |
$text = str_replace('<a href="http://http://', '<a href="http://', $text); | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment