0

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 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!!!

2 Answers 2

3

In this case you can either use a non-greedy quantifier or you can use this alternative regex:

'/<z>([^<]+)<\/z>/'

[^<] captures all characters except <.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I forgot to say it: contents of tags can include < and > characters.
3

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

7 Comments

Using that regex builds the same result as the second one I described in the example.
That's valid, but I would need z to be any kind of XHTML tag. I posted the example using z tag for abstraction purposes, but with z I mean any tag. Using a tag whose name is based on two characters or more with your regex would match in a negative manner the characters that form the name tag, and not the name tag.
@busce11: So, you have to use a xml parser.
I can't use an XML parser. Isn't it possible to modify the second expression you proposed, but instead of saying "one or more characters except for a z" declaring "one or more characters that doesn't include the tag name"?
I tried this: '/<z>(?!.*<z>.*)(.+)<\/z>/U' however, it just matched '4d'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.