簡體   English   中英

使用php regex模式從spf記錄中提取域子域

[英]extract domain subdomain from spf record with php regex pattern

我想使用regex 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>";
}
}

我應該使用哪種正則表達式來提取簡單的域和子域,例如_netblocks.google.com

我聽不清楚您的要求。 但在這里我認為您需要:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM