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
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
Post a Comment