Skip to content

Instantly share code, notes, and snippets.

@detain
Created September 28, 2014 05:28
Show Gist options
  • Save detain/957070237de928da5dc0 to your computer and use it in GitHub Desktop.
Save detain/957070237de928da5dc0 to your computer and use it in GitHub Desktop.
URL detection and split it into parts, each part is optional minus the hostname.
<?php
function is_url($text)
{
if (preg_match('/((?P<protocol>[a-z]{3,6}):\/\/)?((?P<userpass>[a-z]+:[a-z]+|[a-z]+)@)?(?P<hostname>[a-z0-9.-]+\.[a-z]{2,6})(:(?P<port>[0-9]+))?(?P<path>\/.)?/i', $text, $matches))
{
$ret = array();
foreach ($matches as $key => $value)
{
if (!is_numeric($key) && $value != '')
{
$ret[$key] = $value;
}
}
return $ret;
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment