Search code examples
phpregexdnssubdomainspf

extract domain subdomain from spf record with php regex pattern


I want to extract the subdomain & domain name from an spf record using the regex 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>";
}
}

what kind of regex should i use to extract simple domain & subdomain like _netblocks.google.com


Solution

  • I couldn't clearly get what you are asking. But here what I think you need:

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