2013-04-15 14 views
10

Voglio prendere il testo in ordine tra tutti i <span> </span> tag da HTML, ho provato con questo codice, ma restituisce una sola occorrenza:Come ottenere il testo in serie tra tutti i tag <span> da HTML?

preg_match('/<span>(.+?)<\/span>/is', $row['tbl_highlighted_icon_content'], $matches); 

echo $matches[1]; 

mio HTML:

<span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way, 

Il mio codice restituisce solo una occorrenza del tag span, ma voglio ottenere tutto il testo da ogni tag span in HTML sotto forma di un array php.

+4

[Analisi dell'HTML con regex?] (Http://stackoverflow.com/a/1732454/1493698) – Antony

+0

Ho una domanda simile qui http://stackoverflow.com/q/34569269/5477982 –

risposta

2

è necessario passare alla preg_match_all funzione

Codice

$row['tbl_highlighted_icon_content'] = '<span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way,';  

preg_match_all('/<span>.*?<\/span>/is', $row['tbl_highlighted_icon_content'], $matches); 

var_dump($matches); 

come potete vedere ora array è correttamente popolato in modo da poter echo tutte le partite

+0

Grazie Fabio ... risolto il mio problema dalla tua risposta :) –

+0

sei il benvenuto amico! – Fabio

2

uso preg_match_all() è lo stesso, verrà restituito tutte le occorrenze nel $ corrisponde serie

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

+0

Non è possibile analizzare HTML con una regex. [Ever] (http://stackoverflow.com/a/1732454/1493698). – berkes

+1

piace tutto, dipende dal contesto, sta al programmatore sapere come sono i suoi dati, se è solo tag span senza contenuto speciale all'interno, non vedo perché no – aleation

1

qui è il codice per ottenere tutto il valore campata in ordine di

 $str = "<span>The wish to</span> be unfairly treated is a compromise 
attempt that would COMBINE attack <span>and innocen</span>ce. 
Who can combine the wholly incompatible, and make a unity 
of what can NEVER j<span>oin? Walk </span>you the gentle way,"; 

preg_match_all("/<span>(.+?)<\/span>/is", $str, $matches); 


echo "<pre>"; 
print_r($matches); 

si uscita sarà

Array 
(
    [0] => Array 
     (
      [0] => The wish to 
      [1] => and innocen 
      [2] => oin? Walk 
     ) 

    [1] => Array 
     (
      [0] => The wish to 
      [1] => and innocen 
      [2] => oin? Walk 
     ) 

) 

è possibile utilizzare O o 1 Indice

0

Se non ti dispiace usare un componente di terze parti, mi piacerebbe mostrarti il ​​componente Symfony's DomCrawler. È un modo molto semplice per analizzare file HTML/XHTML/XML e navigare attraverso i nodi.

È anche possibile utilizzare selettori CSS. Il tuo codice sarebbe qualcosa di simile:

$crawler = new Crawler($html); 
$spans = $crawler->filter("span"); 
echo $spans[1]->getText();; 

Non hanno nemmeno bisogno di avere un documento completo HTML/XML, se si assegna solo la parte <span>...</span> del codice, che funzionerà bene.

Problemi correlati