使用php regex模式从spf记录中提取域子域

时间:2016-08-07 10:59:06

标签: php regex dns subdomain spf

我想提取子域名&使用正则表达式php的spf记录中的域名。

$spfreccord= ' v=spf1 include:smtproutes.com include:smtpout.com ip4:46.163.100.196 ip4:46.163.100.194 ip4:85.13.135.76 ip4:178.255.156.110 ip4:188.172.204.21 ip4:178.255.154.52 ip4:188.172.233.6 ip4:37.252.230.29 ip4:217.146.22.37 ~all';
spfreccord2="v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all";
$regexdomain = '/\s*([-a-z0-9]+\.)+' . implode("|", $extensionList) . '\s*/i';
preg_match_all($regexdomain, $spfreccord,$matchesdom);
// echo the result



foreach ($matchesdom as $ke) {
foreach ($ke as $domspf) {

    echo $domspf;
    echo "<br>";
}
}

我应该使用什么样的正则表达式来提取简单的域名&amp;子域名如_netblocks.google.com

2 个答案:

答案 0 :(得分:2)

我无法清楚地得到你的要求。但在这里我认为你需要:

$spfreccord ='v=spf1 include:smtproutes.com include:smtpout.com ip4:46.163.100.196 ip4:46.163.100.194 ip4:85.13.135.76 ip4:178.255.156.110 ip4:188.172.204.21 ip4:178.255.154.52 ip4:188.172.233.6 ip4:37.252.230.29 ip4:217.146.22.37 ~all';
$spfreccord2 ="v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all";

$domainExts = ["com", "net", "org", "io"]; // fill according to your needs

$regex = "/:([\w]*?)\\.?([\w]*?)\\.(".implode("|", $domainExts).")/";

preg_match_all($regex, $spfreccord2, $output);
// $output[1] => Subdomains.
// $output[2] => Domains
// $output[3] => Domain extension

var_dump($output);
/*
array(4) {
  [0] =>
  array(3) {
    [0] =>
    string(22) ":_netblocks.google.com"
    [1] =>
    string(23) ":_netblocks2.google.com"
    [2] =>
    string(23) ":_netblocks3.google.com"
  }
  [1] =>
  array(3) {
    [0] =>
    string(10) "_netblocks"
    [1] =>
    string(11) "_netblocks2"
    [2] =>
    string(11) "_netblocks3"
  }
  [2] =>
  array(3) {
    [0] =>
    string(6) "google"
    [1] =>
    string(6) "google"
    [2] =>
    string(6) "google"
  }
  [3] =>
  array(3) {
    [0] =>
    string(3) "com"
    [1] =>
    string(3) "com"
    [2] =>
    string(3) "com"
  }
}
*/

答案 1 :(得分:-1)

      preg_match_all('/(ftp|http|https):\/\/(.*?)[\.][a-zA-Z]{1,3}(.*?)/',$str_search,$arr,PREG_PATTERN_ORDER);
print_r($arr);

域:$ ARR [0] [0]; 子域:$ ARR [2] [0];