Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you strip out the domain name from a URL in php?

Tags:

php

urlparse

People also ask

Which command to extract the domain suffix in php?

PHP's parse_url function makes it easy to extract the domain, path and other useful bits of information from a full URL.

How parse URL in PHP?

PHP | parse_url() Function The parse_url() function is an inbuilt function in PHP which is used to return the components of a URL by parsing it. It parses an URL and return an associative array which contains its various components.

How do I remove http from URL?

To remove http:// or https:// from a url, call the replace() method with the following regular expression - /^https?:\/\// and an empty string as parameters. The replace method will return a new string, where the http:// part is removed. Copied!

What is parse domain?

Splits a hostname into subdomains, domain and (effective) top-level domains. Since domain name registrars organize their namespaces in different ways, it's not straight-forward to split a hostname into subdomains, the domain and top-level domains.


parse_url turns a URL into an associative array:

php > $foo = "http://www.example.com/foo/bar?hat=bowler&accessory=cane";
php > $blah = parse_url($foo);
php > print_r($blah);
Array
(
    [scheme] => http
    [host] => www.example.com
    [path] => /foo/bar
    [query] => hat=bowler&accessory=cane
)

You can use parse_url() to do this:

$url = 'http://www.example.com';
$domain = parse_url($url, PHP_URL_HOST);
$domain = str_replace('www.','',$domain);

In this example, $domain should contain example.com, irrespective of it having www or not. It also works for a domain such as .co.uk


You can also write a regular expression to get exactly what you want.

Here is my attempt at it:

$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://www.example.com/foo/bar?hat=bowler&accessory=cane';
if (preg_match($pattern, $url, $matches) === 1) {
    echo $matches[0];
}

The output is:

example.com

This pattern also takes into consideration domains such as 'example.com.au'.

Note: I have not consulted the relevant RFC.


Here are a couple simple functions to get the root domain (example.com) from a normal or long domain (test.sub.domain.com) or url (http://www.example.com).

/**
 * Get root domain from full domain
 * @param string $domain
 */
public function getRootDomain($domain)
{
    $domain = explode('.', $domain);

    $tld = array_pop($domain);
    $name = array_pop($domain);

    $domain = "$name.$tld";

    return $domain;
}

/**
 * Get domain name from url
 * @param string $url
 */
public function getDomainFromUrl($url)
{
    $domain = parse_url($url, PHP_URL_HOST);
    $domain = $this->getRootDomain($domain);

    return $domain;
}

Following code will trim protocol, domain and port from absolute URL:

$urlWithoutDomain = preg_replace('#^.+://[^/]+#', '', $url);