通过PHP从URL获取.tld

时间:2016-08-24 09:08:16

标签: php url

如何通过PHP从URL获取.tld?

E.g。 www.domain.com/site,PHP应该发布:tld是:.com。

10 个答案:

答案 0 :(得分:14)

使用parse_url()函数获取网址的主机部分然后按.展开并获取数组的最后一个元素

以下示例:

$url = 'http://www.example.com/site';
echo end(explode(".", parse_url($url, PHP_URL_HOST))); // echos "com"

在此之前,最好检查$ url是否是filter_var的实际网址,例如

修改

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

答案 1 :(得分:8)

如果你想从字符串www.domain.com/site中提取主机,那么使用parse_url()是可以接受的解决方案。

但是如果你想要提取域或其部分,你需要使用Public Suffix List的包。是的,您可以使用字符串函数arround parse_url(),但它会产生两个级别TLD的错误结果。

我建议TLDExtract进行域解析,这里是显示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'

答案 2 :(得分:3)

例如,接受的答案不适用于.co.uk。如果您的网址都具有相同的子域或根本没有子域,您可以执行以下操作。

$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 | ";
}

输出: .com | .com | .audio | .co.uk |

答案 3 :(得分:0)

您可以使用preg_matchhttp://php.net/manual/en/function.preg-match.php你应该专注于第三个例子。只需根据您的需求进行调整。

答案 4 :(得分:0)

如果您愿意使用shell_exec并安装了钻孔(或者您也可以使用挖掘): 在PHP中 $ tld = shell_exec(' drill -T $ domain | grep -m1 IN | cut -d.-f 1'); if($ tld!=''){    echo&#t; tld是。'。$ tld; }其他{    echo&#t; tld没有回应&#39 ;; } 它将比先前的建议慢,但它将验证域拼写和正确注册以及其他dns查询好东西。

答案 5 :(得分:0)

简短回答

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

容易!

答案 6 :(得分:0)

如果您不关心带点的LTD(例如“ co.uk”),并且由于触发了通知而不喜欢已经提到的explode + end解决方案,则可以执行此操作。

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

答案 7 :(得分:0)

正如Rid Iculous所说的那样,parse_url()方法不适用于.co.uk或.org.au等双重后缀。

这是一个PHP脚本,用于提取几乎可用的TLD。解决几乎所有案件都是有用的。也就是说,随着TLD随时间变化,使用TLDExtract是一种更可靠的方法。

<?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;
        }
    }
?>

答案 8 :(得分:0)

Utopia Domains库(https://github.com/utopia-php/domains)将基于Mozilla公共后缀列表(https://publicsuffix.org)返回域TLD和公共后缀,它可以用作当前已归档TLDExtract程序包的替代。 / p>

您可以使用'parse_url'函数从URL获取主机名,然后使用Utopia Domains解析器获取正确的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

答案 9 :(得分:-1)

下面的示例演示如何在有或没有URL_SCHEME的情况下使用url

$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;

//输出 xyz