Created
June 21, 2012 17:35
-
-
Save markshust/2967215 to your computer and use it in GitHub Desktop.
Convert a .com or another address to a full-on clickable 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 | |
/** | |
* $url can equal any of the following URL formats: | |
* | |
* http://www.domain.com | |
* https://www.domain.net | |
* http://subdomain.domain.org | |
* www.domain.com/folder | |
* subdomain.domain.net | |
* subdomain.domain.edu/folder/subfolder | |
* domain.net | |
* domain.com/folder | |
*/ | |
$html = preg_replace_callback('#(\s|^)((https?://)?(\w|-)+(\.[a-z]{2,3})+(\:[0-9]+)?(?:/[^\s]*)?)(?=\s|\b)#is', | |
create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2])) return $m[1]."<a href=\"http://".$m[2]."\" target=\"blank\">".$m[2]."</a>"; else return $m[1]."<a href=\"".$m[2]."\" target=\"blank\">".$m[2]."</a>";'), | |
$url); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment