Regular Expression Problem
| From: | Richard Luckhurst | Date: | Wed, 24 Jan 2007 22:27:59 +0000 |
| Subject: | Regular Expression Problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-247723@lists.php.net to get a copy of this message | ||
Hi List
I must be dumb as I have been battling my way through regular expression
examples for a while and I can not work out why the following does not work
properly. I am the first to admit that regular expressions confuse me greatly.
The string is a piece of XML as follows and I have put that string into a
variable called $xml_string
<flights>
<flight flightId="19417361" addAdtPrice="0.0"
addChdPrice="0.0" addInfPrice="0.0">
<legXRefs>
<legXRef legId="23862812"
class="V"></legXRef>
<legXRef legId="23862813"
class="V"></legXRef>
</legXRefs>
</flight>
<flight flightId="19417360" addAdtPrice="0.0"
addChdPrice="0.0" addInfPrice="0.0">
<legXRefs>
<legXRef legId="23862810"
class="V"></legXRef>
<legXRef legId="23862811"
class="V"></legXRef>
</legXRefs>
</flight>
</flights>
What I am trying to do is extract the first <flight ..... /flight> chunk.
I am using the following, based on the example at
http://php.net/manual/en/function.preg-match.php
example 3
preg_match('#<flight .*\/flight>#', $xml_string,$matches);
$tempstr = $matches[0];
What I actually get in $tempstr is everything from the first <flight through to
the last (second) </flight>
I would have expected preg_match to have matched from the first <flight through
to the first </flight>.
I suspect I do not have the regular expression correct. I would great appreciate
any help on sorting out the regular expression so that it only returns the first
chunk of the string.
Regards,
Richard Luckhurst