使用php从文本文件中提取子域/域

时间:2013-07-20 15:31:15

标签: php regex dns

我有以下代码从输入中提取域名并将它们存储在数组中

foreach ($output as $domList)
{
  $extensionList = explode(",", "org,com,net");
  $pattern = '/(\s{0,}|\.)([-a-z0-9]+\.(' . implode("|", $extensionList) . '))\s{1,}/i';
  $matches = array();
  preg_match_all($pattern, $domList, $matches);
}

匹配[0]包含所有提取的域

如何修改它以提取子域?

1 个答案:

答案 0 :(得分:0)

示例输入和预期输出肯定有帮助(我使用输入获得了创作许可)。新正则表达式中的想法是继续吃掉任何不是.com,.org或.net的东西。匹配[0]现在应该产生所有域和子域。

$output = array("a" => " test.com  test.sub.com", "b"=> "a.com a.b.com b.c.a.com" );
foreach ($output as $domList)
{
  $extensionList = explode(",", "org,com,net");
  $pattern = '/\s*([-a-z0-9]+\.)+' . implode("|", $extensionList) . '\s*/i';
  $matches = array();
  preg_match_all($pattern, $domList, $matches);
   //   foreach ($matches[0] as $val) {
   //     echo "matched: " . $val . "\n";
}

根据您的需要调整它应该不难。