开发者

Using PHP regex to parse XML

开发者 https://www.devze.com 2023-04-05 03:11 出处:网络
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>\';

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号