Php 从域名中提取顶级域名
我有一系列顶级域名,如:Php 从域名中提取顶级域名,php,regex,Php,Regex,我有一系列顶级域名,如: ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'] 给定域名,如何根据上面的数组提取域名的顶级域?基本上我不关心这个域有多少层,我只需要提取顶级域 例如: test1.ag->应返回ag test2.com.ag->应返回com.ag test.test2.com.ag->应返回com.ag test3.org->应返回false 感谢函数允许您访问url的主机名。您可以使用它来处理主机名并查找tld $
['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn']
给定域名,如何根据上面的数组提取域名的顶级域?基本上我不关心这个域有多少层,我只需要提取顶级域
例如:
test1.ag->应返回ag
test2.com.ag->应返回com.ag
test.test2.com.ag->应返回com.ag
test3.org->应返回false
感谢函数允许您访问url的主机名。您可以使用它来处理主机名并查找tld
$url = 'http://your.url.com.np';
var_dump(parse_url($url, PHP_URL_HOST));
接下来的步骤可能是使用explode()拆分主机名并检查分解列表中的最后一项。但是我将把这个留给您。函数允许您访问url的主机名。您可以使用它来处理主机名并查找tld
$url = 'http://your.url.com.np';
var_dump(parse_url($url, PHP_URL_HOST));
接下来的步骤可能是使用explode()拆分主机名并检查分解列表中的最后一项。但是我将把这个留给您。函数允许您访问url的主机名。您可以使用它来处理主机名并查找tld
$url = 'http://your.url.com.np';
var_dump(parse_url($url, PHP_URL_HOST));
接下来的步骤可能是使用explode()拆分主机名并检查分解列表中的最后一项。但是我将把这个留给您。函数允许您访问url的主机名。您可以使用它来处理主机名并查找tld
$url = 'http://your.url.com.np';
var_dump(parse_url($url, PHP_URL_HOST));
接下来的步骤可能是使用explode()拆分主机名并检查分解列表中的最后一项。但是我将把这个留给您。使用regexp并不是真正需要的,所以在这里应该避免
function topDomain($url) {
$arr = ['ag', 'asia', 'asia_sunrise', 'com', 'hn'];
$tld = parse_url($url);
$toplevel = explode(".", $tld['path'] );
if(in_array(end($toplevel),$arr)){
return $url;
}
ps.'com.ag'和'org.hn'不是顶级域,而是二级域,因此在示例中忽略了这些域 实际上不需要使用regexp,因此这里应该避免使用
function topDomain($url) {
$arr = ['ag', 'asia', 'asia_sunrise', 'com', 'hn'];
$tld = parse_url($url);
$toplevel = explode(".", $tld['path'] );
if(in_array(end($toplevel),$arr)){
return $url;
}
ps.'com.ag'和'org.hn'不是顶级域,而是二级域,因此在示例中忽略了这些域 实际上不需要使用regexp,因此这里应该避免使用
function topDomain($url) {
$arr = ['ag', 'asia', 'asia_sunrise', 'com', 'hn'];
$tld = parse_url($url);
$toplevel = explode(".", $tld['path'] );
if(in_array(end($toplevel),$arr)){
return $url;
}
ps.'com.ag'和'org.hn'不是顶级域,而是二级域,因此在示例中忽略了这些域 实际上不需要使用regexp,因此这里应该避免使用
function topDomain($url) {
$arr = ['ag', 'asia', 'asia_sunrise', 'com', 'hn'];
$tld = parse_url($url);
$toplevel = explode(".", $tld['path'] );
if(in_array(end($toplevel),$arr)){
return $url;
}
ps.'com.ag'和'org.hn'不是顶级域,而是二级域,因此在示例中忽略了这些域 首先,您应该提供一个按类似域的长度排序的数组,例如“ag”之前的“com.ag”。然后:
function get_domain($s){
$a = ['com.ag', 'ag', 'asia_sunrise', 'asia', 'com', 'org.hn'];
foreach($a as $v){
if(preg_match("/$v$/",$s)){// if it ends with the array's value
return $v;
}
}
return false;// if none matched the pattern, loop ends and returns false
}
echo get_domain('test.test2.com.ag');// com.ag
首先,您应该提供一个按类似域的长度排序的数组,例如“ag”之前的“com.ag”。然后:
function get_domain($s){
$a = ['com.ag', 'ag', 'asia_sunrise', 'asia', 'com', 'org.hn'];
foreach($a as $v){
if(preg_match("/$v$/",$s)){// if it ends with the array's value
return $v;
}
}
return false;// if none matched the pattern, loop ends and returns false
}
echo get_domain('test.test2.com.ag');// com.ag
首先,您应该提供一个按类似域的长度排序的数组,例如“ag”之前的“com.ag”。然后:
function get_domain($s){
$a = ['com.ag', 'ag', 'asia_sunrise', 'asia', 'com', 'org.hn'];
foreach($a as $v){
if(preg_match("/$v$/",$s)){// if it ends with the array's value
return $v;
}
}
return false;// if none matched the pattern, loop ends and returns false
}
echo get_domain('test.test2.com.ag');// com.ag
首先,您应该提供一个按类似域的长度排序的数组,例如“ag”之前的“com.ag”。然后:
function get_domain($s){
$a = ['com.ag', 'ag', 'asia_sunrise', 'asia', 'com', 'org.hn'];
foreach($a as $v){
if(preg_match("/$v$/",$s)){// if it ends with the array's value
return $v;
}
}
return false;// if none matched the pattern, loop ends and returns false
}
echo get_domain('test.test2.com.ag');// com.ag
更新以合并Traxo关于
通配符的观点;我想我的答案有点完整,所以我不说了,但我们基本上得出了相同的解决方案
//set up test variables
$aTLDList = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$sDomain = "badgers.co.uk"; // for example
//build the match
$reMatch = '/^.*?\.(' . str_replace('.', '\.', implode('|', $aTLDList)) . ')$/';
$sMatchedTLD = preg_match($reMatch, $sDomain) ?
preg_replace($reMatch, "$1", $sDomain) :
"";
使用正则表达式可能有点过分,但这是一个简洁的例子。这将在$sMatchedTLD
变量中为TLD matched或提供一个空字符串
诀窍是让第一个*
匹配ungreedy(*?
),否则badgers.com.ag将匹配ag,而不是com.ag更新以包含Traxo关于
通配符的观点;我想我的答案有点完整,所以我不说了,但我们基本上得出了相同的解决方案
//set up test variables
$aTLDList = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$sDomain = "badgers.co.uk"; // for example
//build the match
$reMatch = '/^.*?\.(' . str_replace('.', '\.', implode('|', $aTLDList)) . ')$/';
$sMatchedTLD = preg_match($reMatch, $sDomain) ?
preg_replace($reMatch, "$1", $sDomain) :
"";
$domains = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$str = 'test.test2.com.ag'; //your string
preg_match('/\b('.str_replace('.', '\.', implode('|', $domains)).')$/', $str, $matches);
//replace . with \. because . is reserved in regex for any character
$result = $matches[0] ?: false;
使用正则表达式可能有点过分,但这是一个简洁的例子。这将在$sMatchedTLD
变量中为TLD matched或提供一个空字符串
诀窍是让第一个*
匹配ungreedy(*?
),否则badgers.com.ag将匹配ag,而不是com.ag更新以包含Traxo关于
通配符的观点;我想我的答案有点完整,所以我不说了,但我们基本上得出了相同的解决方案
//set up test variables
$aTLDList = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$sDomain = "badgers.co.uk"; // for example
//build the match
$reMatch = '/^.*?\.(' . str_replace('.', '\.', implode('|', $aTLDList)) . ')$/';
$sMatchedTLD = preg_match($reMatch, $sDomain) ?
preg_replace($reMatch, "$1", $sDomain) :
"";
$domains = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$str = 'test.test2.com.ag'; //your string
preg_match('/\b('.str_replace('.', '\.', implode('|', $domains)).')$/', $str, $matches);
//replace . with \. because . is reserved in regex for any character
$result = $matches[0] ?: false;
使用正则表达式可能有点过分,但这是一个简洁的例子。这将在$sMatchedTLD
变量中为TLD matched或提供一个空字符串
诀窍是让第一个*
匹配ungreedy(*?
),否则badgers.com.ag将匹配ag,而不是com.ag更新以包含Traxo关于
通配符的观点;我想我的答案有点完整,所以我不说了,但我们基本上得出了相同的解决方案
//set up test variables
$aTLDList = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$sDomain = "badgers.co.uk"; // for example
//build the match
$reMatch = '/^.*?\.(' . str_replace('.', '\.', implode('|', $aTLDList)) . ')$/';
$sMatchedTLD = preg_match($reMatch, $sDomain) ?
preg_replace($reMatch, "$1", $sDomain) :
"";
$domains = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$str = 'test.test2.com.ag'; //your string
preg_match('/\b('.str_replace('.', '\.', implode('|', $domains)).')$/', $str, $matches);
//replace . with \. because . is reserved in regex for any character
$result = $matches[0] ?: false;
使用正则表达式可能有点过分,但这是一个简洁的例子。这将在$sMatchedTLD
变量中为TLD matched或提供一个空字符串
诀窍是让第一个*
匹配ungreedy(*?
),否则badgers.com.ag将匹配ag,而不是com.ag
$domains = ['ag', 'asia', 'asia_sunrise', 'com', 'com.ag', 'org.hn'];
$str = 'test.test2.com.ag'; //your string
preg_match('/\b('.str_replace('.', '\.', implode('|', $domains)).')$/', $str, $matches);
//replace . with \. because . is reserved in regex for any character
$result = $matches[0] ?: false;
编辑:在regexp中添加单词边界,$result为字符串或false
编辑:在regexp中添加单词边界,$result为字符串或false
编辑:在regexp中添加单词边界,$result为字符串或false
编辑:在regexp中添加单词边界,$result是您的字符串还是false为什么需要regex?我会找到这个为什么你需要正则表达式?我会找到这个为什么你需要正则表达式?我会找到这个为什么你需要正则表达式?我会用它来查找我没有url,所以我不需要解析它。我知道我可以分解,但顶级域可以是例如“com.ag”,因此即使我访问分解数组中的最后一项,它们也不总是匹配的。在这种情况下,我会