說有人進入以下網址:獲取域名
http://i.imgur.com/a/b/c?query=value&query2=value
而且我想返回:imgur.com
不i.imgur.com
這是代碼,我現在所擁有的
$sourceUrl = parse_url($url);
$sourceUrl = $sourceUrl['host'];
但這返回i.imgur.com
說有人進入以下網址:獲取域名
http://i.imgur.com/a/b/c?query=value&query2=value
而且我想返回:imgur.com
不i.imgur.com
這是代碼,我現在所擁有的
$sourceUrl = parse_url($url);
$sourceUrl = $sourceUrl['host'];
但這返回i.imgur.com
檢查下面的代碼,它應該做的工作的罰款。
<?php
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
print get_domain("http://mail.somedomain.co.uk"); // outputs 'somedomain.co.uk'
?>
if(substr_count($original_url, 'http://')) {
if(substr_count($original_url, 'www.')) {
// url style would be 'http://www.abc.xxx/page?param' or http://www.abc.xxx.xx/page?param
// extract 'abc'
$temp = explode('.', $original_url);
$store_url = $temp[1];
// now
// $temp[2] = xxx or xxx/page?param
// $temp[3] = null or xx/page?param
//if ($temp[3] == null) { // then we are sure that $temp[2]== "xxx/page?param"
if(sizeof($temp) > 3) {
// extract "xxx" from "xxx/page?param" and append to store url so it will be "abc.xxx"
$temp = explode('/',$temp[2]);
$store_url .= '.'.$temp[0];
}
else {
// then we are sure that $temp[2]== "xxx" and then $temp[3] == "xx/page?param"
// or $temp[2]== xxx/page?stripped-link from second dot(.)
if(substr_count($temp[2], '/')) { // in case $temp[2]== xxx/page?stripped-link from second dot(.)
// extract "xxx" from "xxx/page?stripped-link" and appent to store url so it will be "abc.xxx"
$temp = explode('/',$temp[2]);
$store_url .= '.'.$temp[0]; // "abc".="xxx" ==> abc.xxx
}
else { // in case $temp[2]== "xxx" and then $temp[3] == "xx/page?param"
$store_url .= '.'.$temp[2]; // "abc".="xxx" ==> abc.xxx
// extract "xx" from "xx/page?param" and appent to store url so it will be "abc.xxx.xx"
$temp = explode('/',$temp[3]);
if(strlen($temp[0])==2) {
$store_url .= '.'.$temp[0];
}
}
}
}
else {
// url style would be 'http://abc.xxx/page?param' or 'http://abc.xxx.xx/page?param'
// remove 'http://'
$temp = substr($original_url, 7);
// now temp would be either 'abc.xxx/page?param' or 'abc.xxx.xx/page?param'
// explode with '/'
$temp = explode('/', $temp);
$store_url = $temp[0];
}
}
else if(substr_count($original_url, 'www.')) {
// url style would be 'www.abc.xxx/page?param' or 'www.abc.xxx.xx/page?param'
// remove 'www.'
$temp = substr($original_url, 4);
// now, $temp would be either "abc.xxx/page?param" or "abc.xxx.xx/page?param"
// explode with '/'
$temp = explode('/', $temp);
$store_url = $temp[0];
}
else {
// url style would be 'abc.xxx/page?param' or 'abc.xxx.xx/page?param'
//explode with '/'
$temp = explode('/', $original_url);
$store_url = $temp[0];
}
嗯......那是什麼? –
它是從url獲取主機名的功能。原始網址是您的網址和$ store_url返回主機網址.. –
使用本:
$uri = "$_SERVER[REQUEST_URI]";<br>
print($uri);
例子:
http://exemple.com/?directory<br>
Result:
/?diretory
的命令,在目錄中,而不是域名。
該問題涉及用戶輸入的網址,而不是他們訪問的地址。 – robmcvey
如果你只想要的域名,請嘗試以下操作:
$domain = $_SERVER['SERVER_NAME'];
echo $domain;
需要,使用Public Suffix List包。是的,你可以使用字符串函數arround parse_url()或regex,但是它們會在複雜的URL中產生不正確的結果。
我的域名解析建議更換TLDExtract,這裏是示例代碼:
$url = 'http://i.imgur.com/a/b/c?query=value&query2=value';
parse_url($url, PHP_URL_HOST); // will return 'i.imgur.com'
$extract = new LayerShifter\TLDExtract\Extract();
$result = $extract->parse($url);
$result->getFullHost(); // will return 'i.imgur.com'
$result->getSubdomain(); // will return 'i'
$result->getRegistrableDomain(); // will return 'imgur.com'
$result->getSuffix(); // will return 'com'
我一直在使用publicsuffix.org找到了一個非常有用的庫, PHP Domain Parser是PHP實現基於公共後綴列表域解析器。
https://github.com/jeremykendall/php-domain-parser
<?php
// this will do the job
require_once '../vendor/autoload.php';
$pslManager = new Pdp\PublicSuffixListManager();
$parser = new Pdp\Parser($pslManager->getList());
var_dump($parser->getRegistrableDomain('www.scottwills.co.uk'));
?>
串(16) 「scottwills.co.uk」
保健佳品?? – ramo
如何啓動「這是獲取域的真實名稱的方法。」 – 2013-04-16 01:13:28
見:http://stackoverflow.com/questions/288810/get-the-subdomain-from-a-url – duskwuff