Created
August 20, 2012 16:06
-
-
Save cam-gists/3405395 to your computer and use it in GitHub Desktop.
PHP: Convert URLS -> Links
This file contains hidden or 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 text to linked HTML | |
Find text URLs in string and replaces with linked HTML URL. e.g. 'Please go to: http://www.OpenCrypt.com/' would become 'Please go to: http://www.OpenCrypt.com/' | |
Usage: | |
print url_string_to_html("Please go to: http://www.OpenCrypt.com/"); | |
Or: | |
$string = "Please go to: http://www.OpenCrypt.com/"; | |
print url_string_to_html($string); | |
*/ | |
function url_string_to_html($string) { | |
$string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<A TARGET=\"_blank\" HREF=\"$1\">$1</A>",$string); | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment