php - Get all text inside html tag with regex? -


for example, have html:

<strong>this one</strong> <span>test one</span> <strong>this two</strong> <span>test two</span> <strong>this three</strong> <span>test three</span> 

how text inside strong , span regex?

use dom , never use regular expressions parsing html.

$dom = new domdocument; $dom->loadhtml($html); foreach ($dom->getelementsbytagname('strong') $tag) {    echo $tag->nodevalue."<br>";   } foreach ($dom->getelementsbytagname('span') $tag) {     echo $tag->nodevalue."<br>"; } 

output :

this 1 2 3 test 1 test 2 test 3 

demo


why shoudn't use regular expressions parse html content ?

html not regular language , hence cannot parsed regular expressions. regex queries not equipped break down html meaningful parts. many times not getting me. enhanced irregular regular expressions used perl not task of parsing html.

that article our jeff atwood. read more here.


Comments

Popular posts from this blog

php - How to serve a file for download on WordPress -

how to share virtual sharepoint server and get it in host windows and visual studio? -