2012-11-14 15 views
7

ho funzione preg_match_all:preg_match_all in semplice array

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

che funzioni come previsto, ma il problema è che preg_matches tutti gli articoli due volte e in una vasta gamma multidimensionale così per esempio in cui, come previsto , preg_matched tutti i 11 articoli necessari, ma due volte e in un array multidimensionale:

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 
     ) 

) 

Come convertire questo array in semplice e senza quegli elementi duplicati? Grazie mille.

risposta

6

Otterrete sempre una matrice multidimensionale indietro, tuttavia, è possibile avvicinarsi a ciò che si vuole in questo modo:

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 

E se non si desidera che il submatch a tutti, quindi utilizzare un non- raggruppamento cattura:

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 

si noti che questa chiamata a preg_match_all sta usando PREG_PATTERN_ORDER invece di PREG_SET_ORDER:

PREG_PATTERN_ORDER I risultati degli ordini in modo che $ matches [0] sia un array di corrispondenze di pattern completi , $ matches [1] è un array di stringhe corrispondenti a il subpattern prima parentesi e così via.

PREG_SET_ORDER Ordini risultati in modo che $ matches [0] è un array di primo serie di incontri, $ matches [1] è un array di seconda serie di incontri, e così via.

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

+1

Probabilmente doveva essere $ risultati = $ uscita [0]. Grazie Ha funzionato :) – DadaB

+0

@MantasBalaisa Oh, hai ragione! Non sono sicuro di cosa stavo pensando. Grazie. Fisso. – jimp

+0

Potrebbe essere necessario sfuggire alle barre in avanti? – Dan

1

Usa

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

come regex. Se si utilizza un gruppo non acquisibile (che è ciò che significa ?:), un riferimento arretrato non verrà visualizzato nell'array.

Problemi correlati