使用 php 正则表达式模式从 spf 记录中提取域子域

extract domain subdomain from spf record with php regex pattern

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

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

我应该使用哪种正则表达式来提取像 _netblocks.google.com

这样的简单域和子域
      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];

我不太清楚你问的是什么。但我认为您需要的是:

$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"
  }
}
*/