日期:2014-05-17 浏览次数:20545 次
$str=<<<html <ul class="s"> <li id="tongji_cate_1_pn_1_rank_1"> <a href="http://www.tmall.com" class="pic"> <img src="http://image.com/pic.gif"></a> <p><a href="http://www.taobao.com/" class="site">[淘宝商城]</a></p> <p><a href="http://www.tmall.com/" title="活动标题" >活动标题</a></p> </li> <li id="tongji_cate_2_pn_2_rank_2"> …… </li> …… <ul> html; preg_match_all('#<li[^>]*>(.*)</li>#isU',$str,$arr); foreach($arr[1] as $v){ preg_match_all('#<a\s*href="(.*)"[^>]*>(.*)</a>#isU',$v,$ar[]); } print_r($ar);
------解决方案--------------------
$str = <<<HTML <ul> <li id="tongji_cate_1_pn_1_rank_1"> <a href="http://www.tmall.com" class="pic"> <img src="http://image.com/pic.gif1"></a> <p><a href="http://www.taobao.com/" class="site">[淘宝商城1]</a></p> <p><a href="http://www.tmall.com/1" title="活动标题1" >活动标题</a></p> </li> <li id="tongji_cate_2_pn_2_rank_2"> <a href="http://www.tmall.com" class="pic"> <img src="http://image.com/pic.gif2"></a> <p><a href="http://www.taobao.com/" class="site">[淘宝商城2]</a></p> <p><a href="http://www.tmall.com/2" title="活动标题2" >活动标题</a></p> </li> </ul> HTML; /** *"shopname" => "淘宝商城", "activename"=> "活动标题", "actimage" => "http://image.com/pic.gif", "acturl" => "http:/www.tmall.com/" //这个是活动标题的链接 */ $pattern = '~img.*(?<=")(.*)".*\[(.*)\].*(?<=f=")(.*)".*(?<=e=")(.*)"~Uis'; preg_match_all($pattern, $str, $m); $res = array(); $num = 4;//匹配项数量 for ($i = 0; $i < count($m[0]); $i++){ for ($j = 1; $j <= $num; $j++){ $res[$i][] = $m[$j][$i]; } } echo '<pre>'; print_r($res); echo '</pre>'; /** 剩下的你懂的 Array ( [0] => Array ( [0] => http://image.com/pic.gif1 [1] => 淘宝商城1 [2] => http://www.tmall.c