Possible Duplicate:
How to parse and process HTML with PHP?
I’m not very good with regex, but I found this code:
<?php
$string = "some text (a(b(c)d)e) more text";
if(preg_match("/\((?>[^()]+|(?R))*\)/",$string,$matches))
{
echo "<pre>"; print_r($matches); echo "</pre>";
}
?>
And I’m trying to change the regex pattern to match opening and closing html tags instead of parenthesis, but I cant figure out how to mimic "[^()]+" so that it matches tags instead of parenthesis.
The purpose of this would be to allow me to make a new html tag, whose contents I can access regardless of how many times the tag is nested within itself.
Thank you.
[^()]defines character class.^means “everything but following characters”. So your example can be interpreted as everything except brackets.If you’re parsing content of html tag you require
[^<>]+.If you have content like
<div>Blah <a>foo</a>bar</div>and you want to matchBlah <a>foo</a>baryou should use regexp like~<div>(.+?)</div>~?after quantifier is called greedy killer and it’ll make sure regexp “stops eating” when it encouters</divAnyway… You should rather use DOM and
xPath::query()when parsing HTML. Here’s some random tutorial from google.