function - How to get base of a random URL with PHP? -
i need function base of url. (urls submit users, , everything!) example:
https://en.wikipedia.org/wiki/murphy%27s_law => wikipedia.org https://en.m.wikipedia.org/wiki/murphy%27s_law => wikipedia.org http://userpage.chemie.fu-berlin.de/diverse/murphy/murphy_e.html => fu-berlin.de http://www.murphyslawpetone.co.nz/ => murphyslawpetone.co.nz
you need use domain name parser has information ever changing list of public suffixes, incorrectly referred "tlds". otherwise wrong results in cases. suggest take @ jemery kendalls php-domain-parser on github. comes documentation , examples.
<?php require_once __dir__ . '/vendor/autoload.php'; use pdp\publicsuffixlistmanager; use pdp\parser; // obtain instance of parser $pslmanager = new publicsuffixlistmanager(); $parser = new parser($pslmanager->getlist()); $url = 'http://www.murphyslawpetone.co.nz/'; $urlobj = $parser->parseurl($url); $hostobj = $url->host; $baseurl = $host->registerabledomain; echo $baseurl;
this output murphyslawpetone.co.nz
looking for, if got question right...
this approach more reliable typical solutions see:
- it not take whole host name (which not asking for)
- it able handle public suffixes consisting of more single label
you have update list of public suffixes time time, since changes. not on daily base, should update maybe once month...
Comments
Post a Comment