Skip to content

Instantly share code, notes, and snippets.

@markshust
Created June 21, 2012 17:35
Show Gist options
  • Save markshust/2967215 to your computer and use it in GitHub Desktop.
Save markshust/2967215 to your computer and use it in GitHub Desktop.
Convert a .com or another address to a full-on clickable link
<?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