<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(……)
            ……
);就像上面一样输出数组  

解决方案 »

  1.   

    只用正则达到你的要求似乎不太可能。要处理一下。我贴下匹配结果。处理自己补充:   $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);
      

  2.   


    $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
            ))
    */
      

  3.   


    <?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);output:
    array
      0 => 
        array
          'acturl' => string 'http://www.tmall.com' (length=20)
          'actimage' => string 'http://image.com/pic.gif' (length=24)
          'shopname' => string '淘宝商城' (length=12)
          'activename' => string '活动标题' (length=12)
      1 => 
        array
          'acturl' => string 'http://www.tmall22.com' (length=22)
          'actimage' => string 'http://image.com/pic2.gif' (length=25)
          'shopname' => string '京东商城' (length=12)
          'activename' => string '活动标题' (length=12)