Last active
October 22, 2019 06:57
-
-
Save gmarokov/ae53675e15dc30e43d5cef99fa3887f3 to your computer and use it in GitHub Desktop.
PHP snippets for URLs
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
//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