Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.
Popular code excerpts may be moved to "Code Snippets" by the moderators.
function markup($text)
{
$tlds="biz|com|edu|gov|info|mil|mobi|net|org";
function fixurl($matches)
{
$url=$matches[0];
if (isset($matches[3]) && $matches[3]=='@')
return "<a href=\"mailto:{$matches[2]}\">{$matches[0]}</a>";
if (strpos($url,"http://")===false) $url="http://$url";
return "<a href=\"$url\">$matches[0]</a>";
}
$scheme="(?:http://|mailto:)";
$user="(?:[a-zA-Z0-9_.]+)";
$hostname="(?:[a-zA-Z0-9]+\.)+";
$path="(?:/([a-zA-Z0-9$\-_.+!*'(),;:@&=%]*[a-zA-Z0-9])?)*";
$query="(?:\?([a-zA-Z0-9$\-_.+!*'(),;:@&=%]*[a-zA-Z0-9&=\-])?)?";
return preg_replace_callback(
"{($scheme?($user(@)$hostname(?:$tlds))|$scheme?$hostname(?:$tlds)$path$query)}",
"fixurl",$text);
}
// example usage:
echo markup("This is a test.com. Notice it doesn't include (irritating.com/closing?parens), or require www or http prefixes. Email test bob@nowhere.com. ");
It was designed to autolink urls for a newspaper print conversion. In articles, they almost always write "blah.com/sports" without the www hostname most of the time and certainly never including scheme.
1) You should make the require www optional, as many people would only want actual links hyperlinked.
2) There are many more domain names than you included in your $tlds. .ca .it .co.uk etc etc etc.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.