preg_matching an XML node not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jsavagedesign
    New Member
    • Jun 2007
    • 17

    preg_matching an XML node not working

    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>1gskmi sc_400.jpg</JPG_400>
    </sample>

    <sample id="2">
    <Company>GSK</Company>
    <JPG_725>1gskmi sc_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]
  • jsavagedesign
    New Member
    • Jun 2007
    • 17

    #2
    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]

    Comment

    Working...