PHP Getting Domain Name From Subdomain
Stackoverflow Question Archive:
- How to get domain name from url?
- Check if domain equals value?
- How do I get the base url?
print get_domain("http://somedomain.co.uk"); // outputs 'somedomain.co.uk'
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
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;
}
Trying to get subdomain URL using php
Here's a one-liner to get the subdomain:
$subdomain = join('.', explode('.', $_SERVER['HTTP_HOST'], -2))
explode
with the limit parameter of -2
will split the string on each dot, ignoring the last two elements. If there are two or fewer elements, it returns an empty array.
join
will assemble the resulting array back into a dot delimited string. In case you have multiple subdomains set, e.g. foo.bar.domain.com
will return foo.bar
.
If there is no subdomain, it will return an empty string.
How to get domain name from URL with PHP?
Just use built-in php function parse_url
You can filter a subdomain from a hostname like this
$url = 'http://subdomain.whatever.example.com/test.html';
$data = parse_url($url);
$host = $data['host'];
$hostname = explode(".", $host);
$domain = $hostname[count($hostname)-2] . "." . $hostname[count($hostname)-1];
print $domain;
Will output
example.com
If you have an url
with a port, parse_url
will deal with it easily, example
$url = 'http://example.com:88/testing';
$data = parse_url($url);
print_r($data);
Will output
Array
(
[scheme] => http
[host] => example.com
[port] => 88
[path] => /testing
)
And below you check if the hostname is a valid IP address or not
$url = 'http://188.123.44.12/test.php';
$data = parse_url($url);
print_r($data);
$hostIsIpAddress = ip2long($data['host']) !== false;
var_dump($hostIsIpAddress);
Which will output bool(true)
or bool(false)
respectively
Getting domain name without TLD
Group the first part of your 2nd regex into /([^.]+)\.[^.]+$/
and $matches[1]
will be php
Get domain and subdomain from $_SERVER[ SERVER_NAME ] (or other way)
Using
$parts=explode('.', $_SERVER["SERVER_NAME"]);
you can split it by the dots.
Get Sub-domain with PHP
Firstly, to break down your attempt:
$_SERVER['SERVER_NAME']
gives you the full server name, e.g. "subdomain.example.com"substr($_SERVER['SERVER_NAME'],0,4)
gives you the first four characters of that string; it's likely the example you copied from was looking for "www.", which is four characters long, but "subd" isn't going to tell you much===
just compares that two things are identical; since you've just asked for four characters, it's never going to match a string of any other length'/^([^.]+)/'
looks like a Regular Expression (aka "regex") - a pattern to match strings; to use that in PHP, you need the PCRE family of functions, such aspreg_match
andpreg_replace
Next, the tip I always give: break the problem down. Forget for a moment that you know what a sub-domain is, and notice that you have a string, and want all the text up to the first dot.
One way to get that is therefore to split the string on every dot, and take the first part. For that, you would use the excitingly named explode:
$subdomain = explode('.', $fulldomain, 1)[0];
Another way would be using the regex pattern you found, which reads "starting at the beginning, match anything but a dot, at least once, and capture that part". You can actually skip one pair of brackets, because they're grouping the whole pattern, so just '/^[^.]+/'
is enough. To do that, you'd use preg_match, but note that it doesn't return the matched parts, it puts them in an extra array you pass to it:
$matches = null; // will be populated by preg_match
preg_match('/^[^.]+/', $fulldomain, $matches);
$subdomain = $matches[0];
Note: The above will return "stackoverflow" for an input of "stackoverflow.com". That is unavoidable with simple string matching, because there is no standard number of segments that mark a "public domain" - e.g. "bbc.co.uk" is registered at the same level as "bbc.com", but you can't tell that by looking at them. Unless you know in advance what your possible endings will be, you need something that checks the Public Suffix List, such as the php-domain-parser library.
PHP how to get the base domain/url?
Use SERVER_NAME
.
echo $_SERVER['SERVER_NAME']; //Outputs www.example.com
Get domain name without www and .com in PHP
I try this it work for me.
$original_url="http://subdomain.example.co.uk"; //try with all urls above
$pieces = parse_url($original_url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
echo strstr( $regs['domain'], '.', true );
}
Output- example
I get this from Here
Get domain name from full URL
Related Topics
PHP How to Round Down to Two Decimal Places
Byethost Server Passing HTML Values "Checking Your Browser" With Json String
PHP How to Start an External Program Running - Having Trouble With System and Exec
PHP Install Ssh2 on Windows Machine
Ternary Operator and String Concatenation Quirk
Anonymous Recursive PHP Functions
How to Set the Default Value of a Timestamp Column to the Current Timestamp With Laravel Migrations
How to Find the Dates Between Two Specified Date
Strtotime With Different Languages
What Does MySQL_Real_Escape_String() Do That Addslashes() Doesn'T