By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

preg_matching an XML node not working

P: 17
Does anyone know why the following isn't working, I'm losing my mind over this.

[PHP]

//if this is part of the xml file your loading
$str = '<xmlRoot>

<sample id="1">
<Company>GSK</Company>
<JPG_400>1gskmisc_400.jpg</JPG_400>
</sample>

<sample id="2">
<Company>GSK</Company>
<JPG_725>1gskmisc_725.jpg</JPG_725>
</sample>
</xmlroot>';

//this dosn't match the first node only
preg_match('/<sample id\="1">[\S\s]*<\/sample>/',$str,$a);

print_r($a[0]);


[/PHP]
Jan 8 '08 #1
Share this Question
Share on Google+
1 Reply


P: 17
Well, I fianlly got it.

this is the magic pattern:

[PHP]

$tag = 'sample id="1"';
$base = 'sample';

preg_match('/<' . preg_quote($tag) . '>([^`]*?)<\/' . $base . '>/i',$str,$a);

print_r($a[0]);

[/PHP]
Jan 8 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.