7

私は preg_match_all 関数を持っています:

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

意図したとおりに機能していますが、問題は、preg_matches すべてのアイテムを 2 回、次のような巨大な多次元配列にすることです。たとえば、意図したとおり、必要な 11 個のアイテムすべてを preg_matched しますが、2 回多次元配列にします。

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
        )

)

この配列を単純なものに変換し、それらの重複項目をなくすにはどうすればよいですか? どうもありがとうございました。

4

2 に答える 2

8

常に多次元配列が返されますが、次のようにして、必要なものに近づけることができます。

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

サブマッチがまったく必要ない場合は、非キャプチャ グループを使用します。

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

この preg_match_all への呼び出しは、PREG_SET_ORDER の代わりに PREG_PATTERN_ORDER を使用していることに注意してください。

PREG_PATTERN_ORDER $matches[0] が完全なパターン一致の配列、$matches[1] が最初の括弧で囲まれたサブパターンに一致する文字列の配列などになるように結果を並べ替えます。

PREG_SET_ORDER $matches[0] が最初の一致セットの配列、$matches[1] が 2 番目の一致セットの配列、というように結果を並べ替えます。

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

于 2012-11-14T05:48:52.450 に答える
2

使用する

#<h2>(?:.*?)</h2>#is 

あなたの正規表現として。非キャプチャ グループ (?:意味するもの) を使用する場合、後方参照は配列に表示されません。

于 2012-11-14T05:48:44.573 に答える