php - Cari tatasusunan berbilang dimensi yang diperoleh melalui ungkapan biasa
漂亮男人
漂亮男人 2017-05-16 13:01:40
0
3
548
<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'=>tatasusunan(
    tatasusunan('/article/110001.html','/images/5.jpg','文章标题1'),
    tatasusunan('/article/110021.html','/images/51.jpg','文章标题2'),
    ),
    '栏目2'=>susun(
    tatasusunan('/article/130401.html','/images/23.jpg','文章标题3'),
    tatasusunan('/article/114022.html','/images/26.jpg','文章标题4'),
    tatasusunan('/article/123442.html','/images/89.jpg','文章标题5'),
    tatasusunan('/article/12225.html','/images/42.jpg','文章标题6'),
    ),

)

每个栏目下的文章数不固定.
谢谢了.

漂亮男人
漂亮男人

membalas semua(3)
漂亮男人

Ia tidak boleh dicapai dengan menggunakan preg_match_all sahaja, tetapi tatasusunan sedemikian boleh dipasang semula.

Anda juga boleh menggunakan queryList, anda akan mengetahuinya oleh Baidu.

小葫芦

Adalah disyorkan untuk menggunakan queryList tatasusunan anda tidak boleh dilaksanakan menggunakan ungkapan biasa, tetapi anda boleh menyambung tatasusunan sendiri selepas memadankan ungkapan biasa

.
迷茫

Agak menyusahkan, tetapi anda masih boleh mendapatkan hasil yang anda inginkan

<?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);


Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan