Skip to content

Instantly share code, notes, and snippets.

@gmarokov
Last active October 22, 2019 06:57
Show Gist options
  • Save gmarokov/ae53675e15dc30e43d5cef99fa3887f3 to your computer and use it in GitHub Desktop.
Save gmarokov/ae53675e15dc30e43d5cef99fa3887f3 to your computer and use it in GitHub Desktop.
PHP snippets for URLs
//Get host name from URL
preg_match('@^(?:https?://)?([^/]+)@i',
"http://www.php.net/index.html", $matches);
$host = $matches[1];
//Get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
//Check for valid domain
private function isValidDomainName($domainInput)
{
return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domainInput) //valid chars check
&& preg_match("/^.{1,253}$/", $domainInput) //overall length check
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domainInput) ); //length of each label
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment