[英]Extract subdomain / domain from a text file using php
我有以下代碼可從輸入中提取域名並將其存儲在數組中
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);
}
matchs [0]包含所有提取的域
我如何修改它以提取子域?
樣本輸入和預期的輸出肯定會有所幫助(我在輸入中獲得了創意許可)。 新正則表達式中的想法是繼續吞噬非.com,.org或.net的內容。 Matchs [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";
}
根據您的需求進行調整並不難。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.