求一段正则表达式
<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>
我希望通过正则 将上面的内容 变成一串数组
array(
[0] =>array(
"shopname" => "淘宝商城",
"activename"=> "活动标题",
"actimage" => "http://image.com/pic.gif",
"acturl" => "http:/www.tmall.com/" //这个是活动标题的链接
),
[1] =>array(……)
……
);
就像上面一样输出数组
[解决办法]
只用正则达到你的要求似乎不太可能。要处理一下。我贴下匹配结果。处理自己补充:
$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.com/1 [3] => 活动标题1 ) [1] => Array ( [0] => http://image.com/pic.gif2 [1] => 淘宝商城2 [2] => http://www.tmall.com/2 [3] => 活动标题2 ))*/
[解决办法]
<?php$infos = array();$keynames = array('acturl','actimage','shopname','activename');$str = '<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"> <a href="http://www.tmall22.com" class="pic"> <img src="http://image.com/pic2.gif"></a> <p><a href="http://www.360buy.com/" class="site">[京东商城]</a></p> <p><a href="http://www.tmall.com/" title="活动标题" >活动标题</a></p> </li>……<ul>';preg_match_all('/<a\s+href="([^"]+?)"\s+class="pic">.*?<img\s+src="([^"]+?)">.*?<a\s+href="[^"]+"\s+class="site">\[([^\]]+?)\]<\/a>.*?<a\s+href="[^"]+"\s+title="活动标题"\s*>([^<]+)<\/a>/is',$str,$matchs);array_shift($matchs);$row = count($matchs[0]);$col = count($matchs);for($j=0;$j<$row;$j++){ for($i=0;$i<$col;$i++){ $infos[$j][$keynames[$i]] = $matchs[$i][$j]; }}var_dump($infos);