|
发表于 2020-11-28 22:11
来自手机
|
显示全部楼层
arecluse 发表于 2020-11-28 15:45
能给个站内学习链接吗,我学习一下,谢谢
dir遍历文件
正则匹配
下面是php的,也写了一下,供你参考。
<?php
//https://www.php.net/manual/zh/class.recursivedirectoryiterator.php
function get_all_html(){
$Directory = new RecursiveDirectoryIterator('./');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.html$/i',RegexIterator::MATCH);
$arr=[];
foreach($Regex as $r){
$arr[]=$r->getPathname();
}
//print_r(iterator_to_array($Regex));
return $arr;
}
$files=get_all_html();
$i=0;
foreach($files as $k=>$v){
$html=file_get_contents($v);
$patten='~[男女]~u';
if(preg_match($patten,$html,$matches)){
//echo $matches[0];
$result[$i]["性别"]=$matches[0];
}else{
echo $v."<br>";
}
$patten='~1\d{10}~u';
if(preg_match($patten,$html,$matches)){
//echo $matches[0];
$result[$i]["手机号"]=$matches[0];
}else{
echo $v."<br>";
}
$i++;
}
//print_r($result);
$file_out="result.txt";
unlink($file_out);
foreach($result as $v){
$str=implode("\t",$v)."\r\n";
file_put_contents($file_out,$str,FILE_APPEND);
}
$s=file_get_contents($file_out);
echo $s;
die();
?>
|
|