Last active
January 22, 2023 01:33
-
-
Save egulhan/4b2495499cc229b8e6426621993d11b5 to your computer and use it in GitHub Desktop.
Validate domain name or check if a string contains a domain name using PHP
This file contains 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
<?php | |
/** | |
* Checks if string contains a domain name | |
* @param $string | |
* @return false|int | |
*/ | |
function checkIfContainsDomainName($string) | |
{ | |
$pattern = '/(http[s]?\:\/\/)?(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}/'; | |
return preg_match($pattern, $string); | |
} | |
?> |
This file contains 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
<?php | |
/** | |
* Validates domain name | |
* @param $domain | |
* @return false|int | |
*/ | |
function validateDomainName($domain) | |
{ | |
$pattern = '/^(http[s]?\:\/\/)?(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/'; | |
return preg_match($pattern, $domain); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment