0

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

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] 包含提取的所有域

我如何修改它以提取子域?

4

1 回答 1

0

示例输入和预期输出肯定会有所帮助(我获得了输入的创意许可)。新正则表达式的想法是继续吞噬任何不是 .com、.org 或 .net 的内容。Matches[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";
}

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

于 2013-07-23T15:06:59.267 回答