Regular Expression for xml extraction

I have XML files of the following form:

<a>
.
.
.
<b>
<c>variable-length multi-line markup</c>
<c>variable-length multi-line markup</c>
<c>variable-length multi-line markup</c>
.
.
.
<c>variable-length multi-line markup</c>
</b>
.
.
.
</a>

I'm trying to find the easiest way to extract the c elements into an array. I tried using

preg_match_all("'(\<c\>)[\s\S]*(\<\/c\>)'",$xml,$matches);

but $matches[0][0] just grabs everything between the first <c> and the last </c>. I'm wondering if there is some expression I could use to limit the matches to individual sets of <c>...</c>, or if some other method might work.

Thanks.
huenterprisesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobsledbobCommented:

preg_match_all("'(\<c\>)[\s\S]*?(\<\/c\>)'",$xml,$matches);

The addition of the question mark there means "don't be greedy".  Basically, you were matching the first <c> and the very last </c>.  Telling it not to be greedy tries to minimize the matching, rather than maximizing.

By the way, doesn't this work too (and it's a bit shorter).  Do you really need to paranthesize the <c>'s??

preg_match_all("|<c>[\s\S]*?</c>|", $xml, $matches);

More info about greedy at the PHP manual page...

http://www.php.net/manual/en/function.preg-match-all.php

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snoyes_jwCommented:
You might also try using PHP's XML parser
(http://www.php.net/manual/en/ref.xml.php)

You can specify in your open/close tag handler functions to set/unset some flag when you reach tag "c", then test for that flag in your character data handler function to add the contents to an array.
0
huenterprisesAuthor Commented:
Thanks.

preg_match_all("|<c>[\s\S]*?</c>|", $xml, $matches);

That was just what I was needing.
0
jayrodCommented:
where is the best place to learn about preg_match syntax?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.