Reputation: 898
I would like to catch all "dev" tags and their respective content, through php preg_match_all()
but can't get the nested ones.
data:
<dev>aaa</dev>
<dev>bbb</dev>
<dev>
ccc
<dev>ddd</dev>
</dev>
my expression so far:
|<dev>(.*)</dev>|Uis
thanks, for your help, b.
Upvotes: 0
Views: 4495
Reputation: 312
You need to have a recursive matching pattern:
/<dev>(.*|(?R))<\/dev>/i
That will just suck up any nested elements, so if you want to then parse those, you will have to run the function again on $matches[1]
Upvotes: 1
Reputation: 655189
Don’t use regular expressions for parsing. Use a real parser like DOMDocument or SimpleXML:
$xml = simplexml_load_string('<root>'.$str.'</root>');
Upvotes: 7
Reputation: 4244
The *
is a greedy operator, consumes as many characters as possible. You should use the *?
non-greedy version instead to find the smallest possible matches. Maybe regexes are not the best tools to do this.
Upvotes: -1