dptx8888 2012-11-14 05:41
浏览 55
已采纳

preg_match_all成简单数组

I have preg_match_all function:

preg_match_all('#<h2>(.*?)</h2>#is', $source, $output, PREG_SET_ORDER);

It's working as intended, BUT the problem is, it preg_matches all items twice and into a huge multi dimensional array like this for example where it, as intended, preg_matched all 11 items needed, but twice and into a multidimensional array:

Array
(
    [0] => Array
        (
            [0] => <h2>10. <em>Cruel</em> by St. Vincent</h2>
            [1] => 10. <em>Cruel</em> by St. Vincent
        )

    [1] => Array
        (
            [0] => <h2>9. <em>Robot Rock</em> by Daft Punk</h2>
            [1] => 9. <em>Robot Rock</em> by Daft Punk
        )

    [2] => Array
        (
            [0] => <h2>8. <em>Seven Nation Army</em> by the White Stripes</h2>
            [1] => 8. <em>Seven Nation Army</em> by the White Stripes
        )

    [3] => Array
        (
            [0] => <h2>7. <em>Do You Want To</em> by Franz Ferdinand</h2>
            [1] => 7. <em>Do You Want To</em> by Franz Ferdinand
        )

    [4] => Array
        (
            [0] => <h2>6. <em>Teenage Dream</em> by Katie Perry</h2>
            [1] => 6. <em>Teenage Dream</em> by Katie Perry
        )

    [5] => Array
        (
            [0] => <h2>5. <em>Crazy</em> by Gnarls Barkley</h2>
            [1] => 5. <em>Crazy</em> by Gnarls Barkley
        )

    [6] => Array
        (
            [0] => <h2>4. <em>Kids</em> by MGMT</h2>
            [1] => 4. <em>Kids</em> by MGMT
        )

    [7] => Array
        (
            [0] => <h2>3. <em>Bad Romance</em> by Lady Gaga</h2>
            [1] => 3. <em>Bad Romance</em> by Lady Gaga
        )

    [8] => Array
        (
            [0] => <h2>2. <em>Pumped Up Kicks</em> by Foster the People</h2>
            [1] => 2. <em>Pumped Up Kicks</em> by Foster the People
        )

    [9] => Array
        (
            [0] => <h2>1. <em>Paradise</em> by Coldplay</h2>
            [1] => 1. <em>Paradise</em> by Coldplay
        )

    [10] => Array
        (
            [0] => <h2>Song That Get Stuck In Your Head YouTube Playlist</h2>
            [1] => Song That Get Stuck In Your Head YouTube Playlist
        )

)

How to convert this array into simple one and without those duplicated items? Thank you very much.

  • 写回答

2条回答 默认 最新

  • duanmi8349 2012-11-14 05:48
    关注

    You will always get a multidimensional array back, however, you can get close to what you want like this:

    if (preg_match_all('#<h2>(.*?)</h2>#is', $source, $output, PREG_PATTERN_ORDER))
        $matches = $output[0]; // reduce the multi-dimensional array to the array of full matches only
    

    And if you don't want the submatch at all, then use a non-capturing grouping:

    if (preg_match_all('#<h2>(?:.*?)</h2>#is', $source, $output, PREG_PATTERN_ORDER))
        $matches = $output[0]; // reduce the multi-dimensional array to the array of full matches only
    

    Note that this call to preg_match_all is using PREG_PATTERN_ORDER instead of PREG_SET_ORDER:

    PREG_PATTERN_ORDER Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.

    PREG_SET_ORDER Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on.

    See: http://php.net/manual/en/function.preg-match-all.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符