Reputation: 600
I needed to strip the
http://www.
from a domain name and also anything following it such as
/example
so that i would just be left with yourdomain.com
I added the following code to a file:
$domain = HTTP_SERVER;
$domain_name = preg_replace('/^https?:\/\/(?:www\.)?/i', '', $domain);
But if i echo $domain_name I still get a url such as yourdomain.com/testsite
Can anyone see what i have done wrong here as it has not removed the /testsite and i thought i had got this right.
Upvotes: 0
Views: 62
Reputation: 600
This may be a hack that someone will disagree with, but i resolved the problem by using the following code.
$url = HTTP_SERVER;
$parse = parse_url($url);
$domain = $parse['host'];
$domain_name = preg_replace('/(?:www\.)?/i', '', $domain);
echo $domain_name;
If you can see a reason why this should not be used, please feel free to let me know. Always something new to learn :)
Upvotes: 0
Reputation: 8819
use this
$url = 'http://www.example.co.uk/directory/level1/last/page.html';
$parse= parse_url($url);
preg_match ("/\.([^\/]+)/", $parse['host'], $mydomain);
echo $mydomain[1];
Upvotes: 2