Using PHP regex to parse XML
How can I use a regular expression to parse XML?
Let's suppose we have the following:
$string = '<z>1a<z>2b</z>3c<z>4d</z>5e</z>';
preg_match_all('/<z>(.+)<\/z>/', $string, $result_a);
preg_match_all('/<z>(.+)<\/z>/U', $string, $result_b);
preg_match_all($regex, $string, $result_x);
If I run that, then $result_a will 开发者_C百科have the string (among the items of the array):
'1a<z>2b</z>3c<z>4d</z>5e'
In addition, variable $result_b will have the strings (among the items of the array):
'1a<z>2b'
'4d'
Now, I want $result_x
to have '2b' and '4d' separately, among the items of the array.
What should $regex
look like?
Thanks in advance!!!
In this case you can either use a non-greedy quantifier or you can use this alternative regex:
'/<z>([^<]+)<\/z>/'
[^<]
captures all characters except <
.
Use non-greedy quantifier:
'/<z>(.+?)<\/z>/'
___^
or change the dot by a negative character class:
'/<z>([^z]+)<\/z>/'
or
'/<z>([^<>]+?)<\/z>/'
or, much more convenient, use a xml parser
精彩评论