Reputation:
I was wondering of the best way of removing certain things from a domain using PHP.
For example:
"http://mydomain.com/" into "mydomain"
or
"http://mydomain.co.uk/" into "mydomain"
I'm looking for a quick function that will allow me to remove such things as:
"http://", "www.", ".com", ".co.uk", ".net", ".org", "/" etc
Thanks in advance :)
Upvotes: 2
Views: 5576
Reputation: 4335
You can combine parse_url() and str_* functions, but you'll never have correct result if you need cut domain zone (.com, .net, etc.) from your result.
For example:
parse_url('http://mydomain.co.uk/', PHP_URL_HOST); // will return 'mydomain.co.uk'
You need use library that uses Public Suffix List for handle such situations. I recomend TLDExtract.
Here is a sample code:
$extract = new LayerShifter\TLDExtract\Extract();
$result = $extract->parse('mydomain.co.uk');
$result->getHostname(); // will return 'mydomain'
$result->getSuffix(); // will return 'co.uk'
$result->getFullHost(); // will return 'mydomain.co.uk'
$result->getRegistrableDomain(); // will return 'mydomain.co.uk'
Upvotes: 0
Reputation: 655269
To get the host part of a URL use parse_url
:
$host = parse_url($url, PHP_URL_HOST);
And for the rest see my answer to Remove domain extension.
Upvotes: 7
Reputation: 1454
I would str_replace out the 'http://' and then explode the periods in the full domain name.
Upvotes: 1
Reputation: 25122
Could you use string replace?
str_replace('http://', '');
This would strip out 'http://' from a string. All you would have to do first is get the current url of the page and pass it through any string replace you wanted to..
Upvotes: 1