从路径获取域部分[重复]

This question already has an answer here:

If I use __DIV__ I get path something like this:

/home/domaincom/public_html/account/assets

I need to get the domaincom part only, how do I do that?

Thanks

</div>

Ironically, there's actually no 'easy' way to get this with $_SERVER alone.

Instead, first retrieve the path with $_SERVER["DOCUMENT_ROOT"] and use explode() on the /:

<?php

$url = $_SERVER["DOCUMENT_ROOT"];
//$url = "/home/domaincom/public_html/account/assets";
$parts = explode('/', $url);
$domain = $parts[2];
echo $domain; // domaincom

I've created a working example showcasing this here.

Hope this helps! :)