Home » Php » Get .tld from URL via PHP

Get .tld from URL via PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

How to get the .tld from an URL via PHP?

E.g. www.domain.com/site, the PHP should post: tld is: .com.

How to&Answers:

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://www.example.com/site';
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

EDIT:

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

Answer:

If you want extract host from string www.domain.com/site, usage of parse_url() is acceptable solution for you.

But if you want extract domain or its parts, you need package that using Public Suffix List. Yes, you can use string functions arround parse_url(), but it will produce incorrect results with two level TLDs.

I recomend TLDExtract for domain parsing, here is sample code that show diff:

$extract = new LayerShifter\TLDExtract\Extract();

# For 'http://www.domain.com/site'

$url = 'http://www.domain.com/site';

parse_url($url, PHP_URL_HOST); // will return www.domain.com
end(explode(".", parse_url($url, PHP_URL_HOST))); // will return com

$result = $extract->parse($url);
$result->getFullHost(); // will return 'www.domain.com'
$result->getRegistrableDomain(); // will return 'domain.com'
$result->getSuffix(); // will return 'com'

# For 'http://www.domain.co.uk/site'

$url = 'http://www.domain.co.uk/site';

parse_url($url, PHP_URL_HOST); // will return 'www.domain.co.uk'
end(explode(".", parse_url($url, PHP_URL_HOST))); // will return uk, not co.uk

$result = $extract->parse($url);
$result->getFullHost(); // will return 'www.domain.co.uk'
$result->getRegistrableDomain(); // will return 'domain.co.uk'
$result->getSuffix(); // will return 'co.uk'

Answer:

The accepted answer doesn’t work on .co.uk for example. If your url’s all have the same sub-domain OR no sub-domain at all, you can do something like this.

$urls = array(
    'http://domain.com/site/blah/',
    'http://www.domain.com/site/blah/',
    'http://www.domain.audio/site?hithere=1',
    'http://www.domain.co.uk/site?somehing=%2babc',
);
$subdomain = 'www.';
foreach($urls as $url){
    $host = parse_url($url, PHP_URL_HOST);
    $host = str_ireplace($subdomain,'',$host);
    $tld = strstr($host, '.');
    echo "$tld | ";
}

OUTPUT:
.com |
.com |
.audio |
.co.uk |

Answer:

Short answer

$extension = pathinfo($_SERVER['SERVER_NAME'], PATHINFO_EXTENSION);

easy!

Answer:

If you are willing to use shell_exec and have drill installed (or you could probably use dig as well):

In PHP

$tld = shell_exec('drill -T $domain | grep -m1 IN | cut -d . -f 1');
if($tld !=''){
   echo 'tld is .'.$tld;
}else{
   echo 'tld has no response';
}

It will be slower than the prior suggestions but it will verify that the domain is spelled and registered correctly and other dns query goodies.

Answer:

If you don’t care about LTD with dots (like “co.uk”) and you don’t like the already-mentioned explode+end solution because it triggers a notice, you could do this.

array_reverse((explode('.', $url)))[0];

Answer:

As Rid Iculous already said parse_url() methods won’t work with double suffixes as .co.uk or .org.au…

Here is a PHP script to extract almost availables TLDs. It is usefull to resolve almost cases. That said, as TLDs change over time, using TLDExtract is a more robust method.

<?php $tlds = array(".abbvie",".abogado",".abudhabi",".ac",".ac.cn",".ac.il",".ac.in",".ac.jp",".ac.nz",".ac.th",".ac.uk",".accountants",".aco",".ad",".adac",".adm.br",".ads",".adult",".adv.br",".ae",".aeg",".af",".afl",".ag",".agakhan",".agro.pl",".ah.cn",".aid.pl",".airbus",".airforce",".airtel",".akdn",".allfinanz",".ally",".alsace",".alstom",".am",".am.br",".amsterdam",".anquan",".anz",".apartments",".apple",".aq",".aquarelle",".archi",".army",".arpa",".arq.br",".art.br",".arte",".arts.ro",".as",".asia",".asso.fr",".asso.mc",".associates",".at",".atm.pl",".attorney",".auction",".audi",".auto.pl",".autos",".avianca",".aw",".baidu",".band",".bank",".barcelona",".barclaycard",".barefoot",".bargains",".bauhaus",".bbc",".bbs.tr",".bbva",".bcg",".bcn",".beats",".beer",".bentley",".berlin",".bet",".bg",".bi",".bid",".bike",".bingo",".bio.br",".biz.fj",".biz.pl",".biz.tt",".bj",".bj.cn",".black",".blackfriday",".blanco",".blue",".bms",".bn",".bnl",".bnpparibas",".boats",".boehringer",".bom",".bond",".boo",".boots",".bosch",".bostik",".br.com",".broker",".brother",".brussels",".bugatti",".build",".builders",".business",".bzh",".cab",".cal",".cam",".camera",".camp",".cancerresearch",".canon",".capetown",".capital",".car",".cards",".care",".career",".careers",".cars",".casa",".cash",".cat",".catering",".cba",".cd",".ceb",".center",".ceo",".cern",".cf",".cfa",".cfd",".cg",".chanel",".channel",".chat",".cheap",".chintai",".christmas",".chrome",".church",".ci",".cipriani",".city",".cityeats",".cl",".claims",".cleaning",".click",".clinic",".clinique",".clothing",".clubmed",".cm",".cn",".cn.com",".cng.br",".cnt.br",".co",".co.ac",".co.com",".co.gy",".co.id",".co.il",".co.in",".co.jp",".co.lc",".co.nz",".co.om",".co.rs",".co.th",".co.uk",".co.ve",".co.za",".coach",".codes",".coffee",".college",".cologne",".com.ag",".com.br",".com.bs",".com.cn",".com.co",".com.cu",".com.gi",".com.gr",".com.gy",".com.hk",".com.hn",".com.ht",".com.mx",".com.pa",".com.pe",".com.ph",".com.pl",".com.pt",".com.ro",".com.ru",".com.sg",".com.tr",".com.tt",".com.tw",".com.ve",".com.vi",".commbank",".comsec",".condos",".construction",".consulting",".contractors",".cooking",".cookingchannel",".cool",".corsica",".country",".coupons",".courses",".cq.cn",".credit",".creditcard",".creditunion",".cricket",".cruises",".csc",".cuisinella",".cx",".cymru",".cz",".dabur",".dad",".dance",".date",".dating",".datsun",".day",".dclk",".dds",".de.com",".deals",".degree",".delivery",".deloitte",".democrat",".dental",".dentist",".desi",".design",".diamonds",".diet",".digital",".direct",".directory",".discount",".dj",".dk",".dm",".dog",".doha",".domains",".doosan",".dot",".download",".dtv",".dubai",".dunlop",".durban",".dvag",".dz",".ec",".ecn.br",".edeka",".education",".ee",".email",".emerck",".energy",".eng.br",".engineer",".engineering",".enterprises",".epson",".equipment",".ericsson",".ernet.in",".erni",".esp.br",".estate",".etc.br",".eti.br",".eu",".eu.lv",".eurovision",".eus",".events",".everbank",".exchange",".expert",".exposed",".express",".extraspace",".fage",".fail",".fairwinds",".faith",".fan",".fans",".farm",".fashion",".fedex",".feedback",".film",".final",".finance",".financial",".firm.in",".firm.ro",".firmdale",".fish",".fishing",".fit",".fitness",".fj",".flights",".florist",".flowers",".flsmidth",".fm",".fm.br",".fo",".foodnetwork",".football",".forsale",".forum",".fot.br",".foundation",".fresenius",".frl",".frogans",".frontdoor",".fst.br",".fund",".furniture",".futbol",".fyi",".g12.br",".gal",".gallery",".gallo",".gallup",".game",".games",".garden",".gb.com",".gb.net",".gd",".gd.cn",".gdn",".gea",".geek.nz",".gen.in",".gen.nz",".gent",".genting",".gg",".ggee",".gi",".gift",".gifts",".gives",".giving",".gl",".glass",".global",".globo",".gmbh",".gmina.pl",".gmx",".go.jp",".go.th",".gob.mx",".gold",".goldpoint",".golf",".goo",".goodyear",".gp",".gq",".gr",".graphics",".gratis",".green",".gripe",".group",".gs",".gs.cn",".gsm.pl",".guide",".guitars",".guru",".gx.cn",".gy",".gz.cn",".hamburg",".haus",".hb.cn",".hdfcbank",".he.cn",".healthcare",".help",".helsinki",".hermes",".hgtv",".hi.cn",".hiphop",".hisamitsu",".hitachi",".hk",".hk.cn",".hkt",".hl.cn",".hm",".hn",".hn.cn",".hockey",".holdings",".holiday",".homedepot",".homes",".honda",".horse",".host",".hosting",".house",".hr",".ht",".hu",".hu.com",".hyundai",".ibm",".ice",".id",".ie",".ifm",".iinet",".ikano",".il",".im",".imamat",".immo",".immobilien",".in",".in.gr",".in.rs",".in.th",".ind.br",".industries",".inf.br",".infiniti",".info",".info.ec",".info.fj",".info.pl",".info.ro",".info.ve",".ink",".institute",".insurance",".insure",".int",".international",".investments",".io",".ir",".irish",".is",".iselect",".ismaili",".ist",".istanbul",".it",".itv",".iwi.nz",".jaguar",".java",".jcb",".jcp",".je",".jetzt",".jewelry",".jl.cn",".jll",".jobs",".joburg",".jor.br",".jp",".jp.net",".js.cn",".juegos",".k12.il",".k12.tr",".kaufen",".kddi",".ke",".kerryhotels",".kerrylogistics",".kerryproperties",".kh.ua",".ki",".kia",".kim",".kitchen",".kiwi",".kiwi.nz",".kn",".koeln",".kosher",".kr",".krd",".kred",".kuokgroup",".ky",".kyoto",".kz",".la",".lacaixa",".lamborghini",".lamer",".lancaster",".land",".landrover",".lasalle",".lat",".latrobe",".law",".lawyer",".lc",".lease",".leclerc",".legal",".lego",".lel.br",".lexus",".lgbt",".li",".liaison",".lidl",".life",".lifestyle",".lighting",".limited",".limo",".linde",".link",".lipsy",".live",".lixil",".ln.cn",".loan",".loans",".locker",".locus",".lol",".london",".lotte",".lotto",".love",".lt",".ltd",".ltd.uk",".ltda",".lu",".luxe",".luxury",".lv",".ly",".ma",".madrid",".mail.pl",".maison",".makeup",".man",".management",".mango",".maori.nz",".market",".marketing",".markets",".marriott",".mba",".me",".me.uk",".med",".med.br",".media",".media.pl",".meet",".melbourne",".memorial",".men",".menu",".metlife",".mg",".mi.th",".miami",".miasta.pl",".mini",".mk",".ml",".mls",".mn",".mo",".mo.cn",".mobi",".moda",".moe",".mom",".monash",".money",".mortgage",".moscow",".motorcycles",".mov",".movie",".movistar",".ms",".msk.ru",".mtn",".mtpc",".mtr",".mu",".muni.il",".museum",".mutuelle",".mx",".mz",".na",".nadex",".nagoya",".name",".name.fj",".name.tt",".natura",".navy",".nc",".ne.jp",".ne.kr",".nec",".net.ai",".net.au",".net.bb",".net.bo",".net.br",".net.cn",".net.co",".net.fj",".net.gt",".net.gy",".net.hk",".net.hn",".net.il",".net.in",".net.lb",".net.lc",".net.lv",".net.mx",".net.nz",".net.om",".net.pe",".net.pl",".net.pr",".net.ru",".net.th",".net.tr",".net.tt",".net.tw",".net.ua",".net.uk",".net.ve",".netbank",".network",".news",".next",".nextdirect",".nf",".ng",".ngo",".nico",".ninja",".nissay",".nl",".nm.cn",".no",".no.com",".nokia",".nom.br",".nom.co",".nom.es",".nom.ni",".nom.pl",".nom.ro",".norton",".nowruz",".nowtv",".nra",".nrw",".nt.ro",".ntr.br",".nu",".nx.cn",".nyc",".nz",".obi",".odo.br",".off.ai",".okinawa",".olayan",".olayangroup",".ollo",".om",".omega",".one",".ong",".onl",".online",".ooo",".or.jp",".or.th",".oracle",".orange",".org.in",".org.lb",".org.lv",".org.mx",".org.nz",".org.pe",".org.pl",".org.pr",".org.ro",".org.rs",".org.ru",".org.tr",".org.tt",".org.tw",".org.uk",".org.ve",".org.vi",".organic",".origins",".osaka",".ott",".ovh",".paris",".pars",".partners",".parts",".party",".pc.pl",".pccw",".pe",".pet",".pf",".philips",".photo",".photography",".photos",".physio",".pics",".pictures",".pid",".pink",".pioneer",".pizza",".pl",".place",".play",".plc.uk",".plumbing",".plus",".pm",".pnc",".pohl",".poker",".politie",".porn",".post",".pp.ru",".ppg.br",".pr",".press",".presse.fr",".priv.pl",".pro",".pro.br",".pro.fj",".pro.tt",".prod",".productions",".prof",".promo",".properties",".property",".protection",".ps",".psc.br",".psi.br",".pw",".pwc",".qa",".qc.com",".qh.cn",".qpon",".quebec",".quest",".racing",".re",".realestate",".realestate.pl",".realty",".rec.br",".rec.ro",".recipes",".red",".redstone",".rehab",".reise",".reisen",".reit",".rel.pl",".rentals",".repair",".report",".republican",".res.in",".rest",".restaurant",".review",".reviews",".rexroth",".rich",".richardli",".ricoh",".rio",".rip",".ro",".rocks",".rodeo",".rs",".rsvp",".ru",".ruhr",".run",".rw",".rwe",".sa",".sa.com",".saarland",".sale",".salon",".sandvik",".sandvikcoromant",".sanofi",".sap",".sarl",".saxo",".sb",".sbi",".sbs",".sc",".sc.cn",".sca",".scb",".schaeffler",".schmidt",".school",".schule",".schwarz",".science",".scor",".scot",".se",".se.com",".se.net",".seat",".seek",".select",".services",".seven",".sew",".sex",".sexy",".sfr",".sg",".sh",".sh.cn",".shangrila",".sharp",".shaw",".shell",".shia",".shiksha",".shoes",".shop.pl",".shopping",".shouji",".show",".shriram",".si",".sina",".singles",".sk",".ski",".skin",".sklep.pl",".sky",".sl",".slg.br",".sm",".sn",".sn.cn",".sncf",".so",".soccer",".sochi.su",".social",".softbank",".software",".sohu",".solar",".solutions",".sos.pl",".soy",".space",".spb.ru",".spiegel",".spreadbetting",".srl",".st",".stada",".star",".starhub",".statebank",".statoil",".stc",".stockholm",".storage",".store",".store.ro",".studio",".study",".style",".su",".sucks",".supplies",".supply",".support",".surf",".surgery",".sv",".swatch",".swiss",".sx",".sy",".systems",".tab",".taipei",".targi.pl",".tatar",".tattoo",".tax",".taxi",".tc",".tci",".tdk",".team",".tech",".technology",".tel",".telecity",".telefonica",".temasek",".tennis",".teva",".tf",".tg",".th",".theater",".theatre",".tickets",".tienda",".tiffany",".tips",".tires",".tirol",".tj.cn",".tl",".tm",".tm.fr",".tm.mc",".tm.pl",".tm.ro",".tmp.br",".to",".today",".tokyo",".tools",".top",".toray",".toshiba",".total",".tourism.pl",".tours",".town",".toyota",".toys",".trade",".trading",".training",".travel",".travel.pl",".travelchannel",".travelers",".travelersinsurance",".trust",".trv",".tui",".tur.br",".turystyka.pl",".tv",".tv.br",".tvs",".tw",".tw.cn",".tz",".ubs",".ug",".uk",".uk.com",".uk.net",".university",".uno",".uol",".ups",".us",".us.com",".us.org",".uy",".uy.com",".uz",".vacation",".vacations",".vana",".vc",".ve",".vegas",".ventures",".vermögensberater",".vermögensberatung",".versicherung",".vet",".vet.br",".vg",".viajes",".video",".vig",".viking",".villas",".vin",".vip",".virgin",".vision",".vista",".viva",".vlaanderen",".vodka",".vote",".voting",".voto",".voyage",".vu",".wales",".walter",".wang",".warman",".watch",".web.ve",".webcam",".weber",".website",".wed",".wedding",".wf",".whoswho",".wien",".wiki",".win",".wine",".wme",".wolterskluwer",".woodside",".works",".world",".ws",".wtc",".wtf",".www.ro",".xerox",".xihuan",".xin",".xj.cn",".xn--3ds443g",".xn--6frz82g",".xn--fiq228c5hs",".xxx",".xyz",".xz.cn",".yachts",".yn.cn",".yodobashi",".yoga",".yt",".yun",".za.com",".zara",".zip",".zj.cn",".zlg.br",".zm",".zone",".zuerich"," ",".academy",".active",".actor",".aero",".agency",".android",".app",".art",".au",".audio",".auto",".bar",".be",".best",".bio",".biz",".blog",".boutique",".br",".buy",".buzz",".bw",".by",".bz",".ca",".cafe",".casino",".cc",".ch",".club",".com",".com.au",".com.es",".com.fr",".community",".company",".compare",".computer",".de",".es",".eu.com",".fi",".forex",".fr",".net",".org",".org.ai",".org.au",".org.bb",".org.br",".org.cn",".org.es",".org.gt",".org.hk",".org.hn",".org.il",".pt",".pub"); $url = "https://tredif.academy.dgftuiopk.co.uk/directory/subdirectory/file.jpeg"; $tld = extractTLD($url,$tlds); if($tld!==false){ echo "TLD is ".$tld; } else { echo "unable to identify TLD"; } function extractTLD($url,$tlds){ $comp = parse_url($url); $candidates = explode(".",$comp["host"]); $candidates = array_reverse($candidates); $frst = ".".$candidates[1].".".$candidates[0]; if(in_array($frst,$tlds)){ return $frst; } else if(in_array($secd,$tlds)){ return $candidates[0]; } else { return false; } } ?> 

Answer:

Utopia Domains library (https://github.com/utopia-php/domains) will return the domain TLD and public suffix based on Mozilla public suffix list (https://publicsuffix.org), it can be used as an alternative to the currently archived TLDExtract package.

You can use ‘parse_url’ function to get the hostname from your URL and than use Utopia Domains parser to get the correct TLD:

<?php require_once './vendor/autoload.php'; use Utopia\Domains\Domain; $url = 'http://demo.example.co.uk/site'; $domain = new Domain(parse_url($url, PHP_URL_HOST)); // demo.example.co.uk var_dump($domain->get()); // demo.example.co.uk var_dump($domain->getTLD()); // uk var_dump($domain->getSuffix()); // co.uk var_dump($domain->getName()); // example var_dump($domain->getSub()); // demo var_dump($domain->isKnown()); // true var_dump($domain->isICANN()); // true var_dump($domain->isPrivate()); // false var_dump($domain->isTest()); // false 

Answer:

You can use preg_match. http://php.net/manual/en/function.preg-match.php You should focus on the 3rd example. Just adjust to your needs.

Answer:

Below example demonstrate using of url with or without URL_SCHEME

$url = "https://www.abc.xyz/test"; //With https URL_SCHEME $url2 = "www.abc.xyz/test"; //Without https URL_SCHEME $tld = parse_url($url); $tld = (isset($tld['scheme']) ? $tld['host'] : $tld['path']); $tld = explode('.',$tld); $tld = end($tld); echo $tld; 

// outputs
xyz