Help extracting data

Discussion in 'PHP' started by bigmike7801, Jan 25, 2010.

  1. #1
    I'm trying to extract the data and place them into variables from the below xml document from the line that says :<day0 name="Monday" date="01/25" max="50" min="41" code="13" phrase="Showers"/>

    basically I would like it to be like $name=Monday, $max=50 etc...

    I'm not sure if this would be a job for preg_match or something else and if so, what would be the best way to do this?

    Thanks for any help!

    
    <data>
    <provider>qwikcast.com</provider>
    <date>01/25/2010</date>
    <time>11:05</time>
    <loc city="Longview" state="WA" country="US" county="Cowlitz" lat="46.1300" lon="-122.9300" elev="21"/>
    <icon dir="NA"/>
    −
    <fcst>
    <day0 name="Monday" date="01/25" max="50" min="41" code="13" phrase="Showers"/>
    <day1 name="Tuesday" date="01/26" max="49" min="37" code="12" phrase="PM Showers"/>
    <day2 name="Wednesday" date="01/27" max="49" min="35" code="2" phrase="Partly Cloudy"/>
    <day3 name="Thursday" date="01/28" max="51" min="36" code="16" phrase="Light Rain"/>
    <day4 name="Friday" date="01/29" max="50" min="42" code="16" phrase="Light Rain"/>
    </fcst>
    <obs id="KKLS" time="10:35 AM" code="1" phrase="Clear" tempUnit="F" temp="46" appTemp="46" appWord="Apparent" dew="42" rh="87" windSpd="11" windDir="SSE" windUnit="mph" vis="10" visUnit="mi" baro="29.91" baroUnit="in"/>
    </data>
    
    Code (markup):
     
    bigmike7801, Jan 25, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Here, try this:

    <?php
    
    $data = <<<XML
    <data>
    <provider>qwikcast.com</provider>
    <date>01/25/2010</date>
    <time>11:05</time>
    <loc city="Longview" state="WA" country="US" county="Cowlitz" lat="46.1300" lon="-122.9300" elev="21"/>
    <icon dir="NA"/>
    -
    <fcst>
    <day0 name="Monday" date="01/25" max="50" min="41" code="13" phrase="Showers"/>
    <day1 name="Tuesday" date="01/26" max="49" min="37" code="12" phrase="PM Showers"/>
    <day2 name="Wednesday" date="01/27" max="49" min="35" code="2" phrase="Partly Cloudy"/>
    <day3 name="Thursday" date="01/28" max="51" min="36" code="16" phrase="Light Rain"/>
    <day4 name="Friday" date="01/29" max="50" min="42" code="16" phrase="Light Rain"/>
    </fcst>
    <obs id="KKLS" time="10:35 AM" code="1" phrase="Clear" tempUnit="F" temp="46" appTemp="46" appWord="Apparent" dew="42" rh="87" windSpd="11" windDir="SSE" windUnit="mph" vis="10" visUnit="mi" baro="29.91" baroUnit="in"/>
    </data>
    XML;
    
    
    
    if(preg_match('|<day[0-9]* name="(.*)" date="(.*)" max="(.*)" min="(.*)" code="(.*)" phrase="(.*)"/>|Ui', $data, $matches)){
    
    
    $name = $matches[1];
    
    $date = $matches[2];
    
    $max = $matches[3];
    
    $min = $matches[4];
    
    $code = $matches[5];
    
    $phrase = $matches[6];
    
    } else {
    
    echo "No matches found";
    
    }
    
    ?>
    PHP:
     
    danx10, Jan 25, 2010 IP