Connecting Tech Pros Worldwide Forums | Help | Site Map

preg_matching an XML node not working

Newbie
 
Join Date: Jun 2007
Posts: 17
#1: Jan 8 '08
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]

Newbie
 
Join Date: Jun 2007
Posts: 17
#2: Jan 8 '08

re: preg_matching an XML node not working


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]
Reply