fr3d fr3d - 1 year ago 157
PHP Question

Get .tld from URL via PHP

I need a short way to get the .tld from an URL via PHP.
E.g., the PHP should post: tld is: .com.
I hope you guys can help me.


Answer Source

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

Example below:

$url = '';
echo end(explode(".", parse_url($url, PHP_URL_HOST))); // echos "com"

Before that it would be nice to check if $url is actual URL with filter_var for example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download