Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text in array between all <span> tag from HTML?

I want to fetch text in array between all <span> </span> tag from HTML, I have tried with this code but it returns only one occurrence :

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

echo $matches[1]; 

My 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,

My code returns only one occurrence of span tag, but I want get all text from every span tag in HTML in the form of a php array.

like image 577
Wiram Rathod Avatar asked Apr 15 '13 12:04

Wiram Rathod


1 Answers

you need to switch to preg_match_all function

Code

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

as you can see now array is correctly populated so you can echo all your matches

like image 120
Fabio Avatar answered Oct 20 '22 18:10

Fabio