我有一些文件(大约500个文件)没有扩展名。
但我设法查看了它的内容,它有一些奇怪的标签和东西。
我需要从中提取所有的IP地址。对于第2行中的ex,总是有一个这样的IP地址…(71.129.195.163)
也有一些html标签,如在很多行中。我需要得到这个域名,比如xyz.com
。
谁能帮助这个PHP新手?我知道得到整个文件作为一个字符串和所有的…但由于php是强大的,我正在寻找一个甜蜜和简单的方法来实现这一点。
Thanks to lot
正则表达式是很好的选择。
查找文件中所有ip地址:
$ipPattern = '/(?:25[0-5]|2[0-4]'d|1'd'd|[1-9]'d|'d)(?:[.](?:25[0-5]|2[0-4]'d|1'd'd|[1-9]'d|'d)){3}/';
$ips = array();
preg_match_all($ipPattern, $fileContents, $ips);
$ips = $ips[0];
查找所有链接:
$linkPattern = '/href('s+)?'=('s+)?[''"](.+?)[''"]/';
$links = array();
preg_match($linkPattern, $fileContents, $links);
$link = $links[3];
假定文件内容在$fileContents中。对每个文件运行此代码。如果你需要收集所有的ip和域名,你可以将它们合并成大数组:
$allIps = array();
$allLinks = array();
// after each run of the above code do:
$allIps = array_merge($allIps, $ips);
$allLinks[] = $link;