Search code examples
phpurlget

Q: How to check the extension of an URL website?


I would like to know if there is a way to check the extension of an URL website ? For example, do something when the website is like http://example.es/ and do another thing when the website is like http://example.fr/

I have check that there is something like

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

which returns the current URL of the web page.

Thanks for help.


Solution

  • Use parse_url() function to get host part of the url then explode by . and get last element of an array

    Example below:

        $url =  'http://' . $_SERVER['SERVER_NAME']; 
        echo end(explode(".", parse_url($url, PHP_URL_HOST))); 
        // echos "com"