本文主要是介绍正则在小偷程序中的应用(续),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//获取资源信息$content = file_get_contents("http://list.sososteel.com/qg/list.html?pg=1&h=".time());/*对抓取的信息进行处理。取class为listTable的表信息。[^<>]匹配除<>外的所有字符。*?用的是懒惰限定符,代表重复任意次,但尽量少重复。即匹配最短的以<table开始的,以class="listTable"为结束的字符串。如aabbcc字符串,如果用懒惰限定符匹配的话应该是aab。如果用贪婪匹配的话应该aabb。*/preg_match("/<table [^<>]*? class=\\\"listTable\\\">(.*?)<\/table>/s",$content,$out);/*抓取tbody的内容,重复一次或多次*/preg_match_all("/<tbody>(.+?)<\/tbody>/s",$out[1],$outData);if(empty($outData[1])){return null;}else{$out_arr=array();//循环输出抓取的内容foreach($outData[1] as $i=>$key){//取td的内容preg_match_all("/<td.*?>(.+?)<\/td>/s",$key,$td);if(count($td[1])==7){foreach($td[1] as $j=>$em){//$em = iconv("gb2312","utf-8",$em);switch($j){case 0:continue;break;case 1:continue;break;case 2:preg_match_all("/<a.*?>(.+?)\<\/a>/s",$em,$name);$out_arr[$i]['name']=$name[1][0];preg_match_all("/<span.*?>(.+?)\<\/span>/s",$em,$time);$out_arr[$i]['time']=$time[1][0];preg_match_all("/<div class=\\\"listText\\\">(.+?)<br \/>/s",$em,$other);if(empty($other[1])){$out_arr[$i]['spec']=null;$out_arr[$i]['mater']=null;$out_arr[$i]['factory']=null;}else{$oth=explode(" ",implode('',$other[1]));$out_arr[$i]['spec']=trim($oth[0]);$out_arr[$i]['mater']=trim($oth[1]);$out_arr[$i]['factory']=trim($oth[2]);}$out_arr[$i]['company']=$name[1][1];continue;break;case 3:$out_arr[$i]['city']=trim($em);continue;break;case 4:$out_arr[$i]['price']=trim(strip_tags($em));continue;break;}}} elsecontinue;}}print_r($out_arr);
这篇关于正则在小偷程序中的应用(续)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!