Skip to content

Instantly share code, notes, and snippets.

@egulhan
Last active January 22, 2023 01:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egulhan/4b2495499cc229b8e6426621993d11b5 to your computer and use it in GitHub Desktop.
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
<?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);
}
?>
<?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