Skip to content

Instantly share code, notes, and snippets.

@zidansec
Created August 1, 2023 21:06
Show Gist options
  • Save zidansec/8d204ae136a922949824bc8bde613792 to your computer and use it in GitHub Desktop.
Save zidansec/8d204ae136a922949824bc8bde613792 to your computer and use it in GitHub Desktop.
Simple PHP code for parsing domains, automatically fetching the TLD of the domain or subdomain.
function get_domain($url) {
$str = '/^(?:https?.\/\/)?(?:[^@\/\n]+@)?(?:)?([^:\/?\n]+).*/m';
preg_match_all($str, $url, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $key => $value) {
// code...
//var_dump($value);
return $value[1];
}
}
// get_domain("https://www.domain.com/login/admin.php?login&username=admin&password=admin&submit=login");
echo get_domain("https://www.domain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // www.domain.com
echo get_domain("https://domain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // domain.com
echo get_domain("http://www.domain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // www.domain.com
echo get_domain("http://domain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // domain.com
echo get_domain("www.domain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // www.domain.com
echo get_domain("domain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // domain.com
echo get_domain("www.dom-ain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // www.dom-ain.com
echo get_domain("dom-ain.com/login/admin.php?login&username=admin&password=admin&submit=login"); // dom-ain.com
echo get_domain("https://www.domain.com/demo/assets/css/"); // www.domain.com
echo get_domain("https://domain.com/demo/assets/css/"); // domain.com
echo get_domain("http://www.domain.com/demo/assets/css/"); // www.domain.com
echo get_domain("http://domain.com/demo/assets/css/"); // domain.com
echo get_domain("www.domain.com/demo/assets/css/"); // www.domain.com
echo get_domain("domain.com/demo/assets/css/"); // domain.com
echo get_domain("www.dom-ain.com/demo/assets/css/"); // www.dom-ain.com
echo get_domain("dom-ain.com/demo/assets/css/"); // dom-ain.com
echo get_domain("https://www.domain.com"); // www.domain.com
echo get_domain("https://domain.com"); // domain.com
echo get_domain("http://www.domain.com"); // www.domain.com
echo get_domain("http://domain.com"); // domain.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment