我收到一段包含几个 url 的文本。我想提取文本中给出的 url 中存在的主机列表。例如:
输入:
www.jenniferlopezonline.com www.jenniferlopez.com www.twitter.com/jlo www.myspace.com/jenniferlopez www.youtube.com/jenniferlopez
期望的输出:
Array(
[0]=>www.jenniferlopezonline.com
[1]=>www.jenniferlopez.com
[2]=>www.twitter.com
[3]=>www.myspace.com
[4]=>www.youtube.com
);
我试过这段代码:
preg_match_all('/([a-z0-9\_\-]+(\.)[a-z0-9\_\.\-]+)/i',$text,$matches);
print_r($matches);
输出有点像这样:
Array
(
[0] => Array
(
[0] => www.jenniferlopezonline.com
[1] => www.jenniferlopez.com
[2] => www.twitter.com
[3] => www.myspace.com
[4] => www.youtube.com
)
[1] => Array
(
[0] => www.jenniferlopezonline.com
[1] => www.jenniferlopez.com
[2] => www.twitter.com
[3] => www.myspace.com
[4] => www.youtube.com
)
[2] => Array
(
[0] => .
[1] => .
[2] => .
[3] => .
[4] => .
)
);
将此输出转换为所需的输出将花费我更多的代码,这会使过程变慢。我会批量获取数据,所以我买不起。