How to Strip Out the Domain Name from a Url in PHP

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

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
)

PHP Strip domain name from url

function test($url) 
{
// Check if the url begins with http:// www. or both
// If so, replace it
if (preg_match("/^(http:\/\/|www.)/i", $url))
{
$domain = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
}
else
{
$domain = $url;
}

// Now all thats left is the domain and the extension
// Only return the needed first part without the extension
$domain = explode(".", $domain);

return $domain[0];
}

PHP Remove http and web domain from url

Here with parse_url() function.

parse_url('http://www.loadedcamp.net/music/63637-adele-hello', PHP_URL_PATH);

Output: /music/63637-adele-hello

Parsing domain from a URL

Check out parse_url():

$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
echo $parse['host']; // prints 'google.com'

parse_url doesn't handle really badly mangled urls very well, but is fine if you generally expect decent urls.

REGEX remove any kind of domain from a URL

Use the following:

^(?:\S+\.\S+?\/|\/)?(\S+)$

The capturing group contains the required text. I am using alternation between the presence of a domain or not, and then capturing the rest of the part.

Demo

Remove .com, .net etc. from URL string

If you already have the domain then simply explode the domain and take the first element of the array.

$domain = 'apple.com';
$domain_parts = explode('.', $domain);
echo $domain_parts[0]; // returns apple

Note that the above will not account for subdomains. I.e. 'www.apple.com' would return 'www'. But based on what you have asked above, explode may be adequate.

If you don't already have the domain then you can use PHP's parse_url function to extract the domain (host) from the URL.

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

Get domain name from full URL

Check the code below, it should do the job fine.

<?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;
}

print get_domain("http://mail.somedomain.co.uk"); // outputs 'somedomain.co.uk'

?>


Related Topics



Leave a reply



Submit