![]() |
Simple PHP Fucnction to extract Domain Name from full urls. |
<?php
function get_domain($url)
{ $pieces = parse_url($url); $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path']; if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { return $regs['domain']; } return false;
}
?>
<?php
include("function.php");
$domain_name = 'https://horje.com/learn/511/how-to-set-html-autofocus-attribute-in-input';
$domain_name = get_domain($domain_name);
echo $domain_name;
// Result: horje.com
?>
<?php
function get_domain($url)
{ $pieces = parse_url($url); $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path']; if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { return $regs['domain']; } return false;
}
?>
<?php
$domain_name = 'https://horje.com/learn/511/how-to-set-html-autofocus-attribute-in-input';
$domain_name = get_domain($domain_name);
echo $domain_name;
// Result: horje.com
?>
Category
: |
Web Tutorial |
Sub Category
: |
PHP Extract Tutorial |
Uploaded by
: |
Admin |