首頁  >  問答  >  主體

php - 求一個透過正規獲取多維數組

<p class="cate">栏目1</p>
<p class="article-list">
    <ul>
        <li>
            <a href="/article/110001.html">
                <p class="article-img">
                    <img src="" src="/images/5.jpg" />
                </p>
                <p class="article-name">文章标题1</p>
            </a>
        </li>
        <li>
            <a href="/article/110021.html">
                <p class="article-img">
                    <img src="" src="/images/51.jpg" />
                </p>
                <p class="article-name">文章标题2</p>
            </a>
        </li>
    </ul>
</p>
<p class="cate">栏目2</p>
<p class="article-list">
    <ul>
        <li>
            <a href="/article/130401.html">
                <p class="article-img">
                    <img src="" src="/images/23.jpg" />
                </p>
                <p class="article-name">文章标题3</p>
            </a>
        </li>
        <li>
            <a href="/article/114022.html">
                <p class="article-img">
                    <img src="" src="/images/26.jpg" />
                </p>
                <p class="article-name">文章标题4</p>
            </a>
        </li>
        <li>
            <a href="/article/123442.html">
                <p class="article-img">
                    <img src="" src="/images/89.jpg" />
                </p>
                <p class="article-name">文章标题5</p>
            </a>
        </li>
        <li>
            <a href="/article/12225.html">
                <p class="article-img">
                    <img src="" src="/images/42.jpg" />
                </p>
                <p class="article-name">文章标题6</p>
            </a>
        </li>
    </ul>
</p>

.....此處省略相同的程式碼

看上面的程式碼.想透過preg_match_all取得一個多維數組(含欄位,文章)
想得類似結果如下:

array(
    '栏目1'=>array(
    array('/article/110001.html','/images/5.jpg','文章标题1'),
    array('/article/110021.html','/images/51.jpg','文章标题2'),
    ),
    '栏目2'=>array(
    array('/article/130401.html','/images/23.jpg','文章标题3'),
    array('/article/114022.html','/images/26.jpg','文章标题4'),
    array('/article/123442.html','/images/89.jpg','文章标题5'),
    array('/article/12225.html','/images/42.jpg','文章标题6'),
    ),

)

每個欄位下的文章數不固定.
謝謝了.

漂亮男人漂亮男人2736 天前533

全部回覆(3)我來回復

  • 漂亮男人

    漂亮男人2017-05-16 13:03:40

    如果單用preg_match_all 的話是實作不了的,但是可以重新組合這樣的陣列。

    也可以使用queryList ,百度一下就知道。

    回覆
    0
  • 高洛峰

    高洛峰2017-05-16 13:03:40

    推薦使用queryList 你這個數組使用正規實作不了不過可以正規匹配後自己拼接數組也是可以的

    回覆
    0
  • 迷茫

    迷茫2017-05-16 13:03:40

    有點麻煩,不過還是配對到你想要的結果

    <?php
    $str = '<p class="cate">栏目1</p>
    <p class="article-list">
        <ul>
            <li>
                <a href="/article/110001.html">
                    <p class="article-img">
                        <img src="" src="/images/5.jpg" />
                    </p>
                    <p class="article-name">文章标题1</p>
                </a>
            </li>
            <li>
                <a href="/article/110021.html">
                    <p class="article-img">
                        <img src="" src="/images/51.jpg" />
                    </p>
                    <p class="article-name">文章标题2</p>
                </a>
            </li>
        </ul>
    </p>
    <p class="cate">栏目2</p>
    <p class="article-list">
        <ul>
            <li>
                <a href="/article/130401.html">
                    <p class="article-img">
                        <img src="" src="/images/23.jpg" />
                    </p>
                    <p class="article-name">文章标题3</p>
                </a>
            </li>
            <li>
                <a href="/article/114022.html">
                    <p class="article-img">
                        <img src="" src="/images/26.jpg" />
                    </p>
                    <p class="article-name">文章标题4</p>
                </a>
            </li>
            <li>
                <a href="/article/123442.html">
                    <p class="article-img">
                        <img src="" src="/images/89.jpg" />
                    </p>
                    <p class="article-name">文章标题5</p>
                </a>
            </li>
            <li>
                <a href="/article/12225.html">
                    <p class="article-img">
                        <img src="" src="/images/42.jpg" />
                    </p>
                    <p class="article-name">文章标题6</p>
                </a>
            </li>
        </ul>
    </p>';
    $returnarr = array();
    $str = preg_replace("/[\t\n\r]+/","",$str);  
    
    preg_match_all('/<p class="cate">(.*?)<\/p>/',$str,$rs);
    preg_match_all('/<ul>(.*?)<\/ul>/',$str,$rscont);
    foreach($rs[1] as $key=>$titleval) {
        
        preg_match_all('/<a href="([^<>]+)">.*?<img src="" src="([^<>]+)".*?\/>.*?<p .*?>(.*?)<\/p>.*?<\/a>/',$rscont[1][$key],$list);unset($list[0]);
        $content = array();
        foreach($list[1] as $key=>$val) {
            $cont = array($val,$list[2][$key],$list[3][$key]);
            $content[] = $cont;
        }
        $returnarr[$titleval] = $content;    
    }
    print_r($returnarr);
    
    
    

    回覆
    0
  • 取消回覆