0

I have this code:

<code>
    <div id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist">
        <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-state-focus" role="tab"
            aria-expanded="false" tabindex="0">
            <a><img class="img" alt="NOS" src="/images/small-nos.png"><span>10:00</span>NOS</a></h3>

        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" role="tabpanel"
             style="display: none; ">
            <a href="/news1276" target="_blank">Today's headlines</a>

            <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-state-focus" role="tab"
                aria-expanded="false" tabindex="0">
                <a><img class="img" alt="SBS" src="/images/small-sbs.png"><span>11:00</span>SBS</a></h3>

            <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" role="tabpanel"
                 style="display: none; ">
                <a href="/news1278" target="_blank">Nieuws</a>
            </div>
        </div>
</code>

What I am doing is the following:

    // parse the html into a DOMDocument
    $dom = new DOMDocument();
    @$dom->loadHTML($html);

    $xpath = new DOMXPath($dom);
    $accordions = $xpath->query("//div[@id='accordion']/div");

// Iterate events
for($i = 0; $i < ($accordions->length); $i++) {

    // The node
    $event = $accordions->item($i);

    // The urls of the node
    $urls = $xpath->evaluate('.//a/@href', $event);

    if ($urls->length > 0) 
    {
        $result[$i] .= $urls->item(0)->value;
    }
}

which gets me the urls I want. I am however also trying to get all the elements in the <h3>'s, I'd like to retrieve the image and all span's within those h3's as it iterates but this is giving me headaches as I have tried all sorts of things. Can anyone please shed some light?

Thanks,

Al

Gordon
  • 312,688
  • 75
  • 539
  • 559
cana
  • 3
  • 1
  • 1
  • 1
    All you need to do is traverse the DOM tree. Check the documentation about node properties like parentNode, childNodes, nextSibling, etc of http://php.net/manual/en/class.domnode.php for. Also nake sure you understood the basic concept: http://stackoverflow.com/questions/4979836/noob-question-about-domdocument-in-php/4983721#4983721 – Gordon Mar 04 '13 at 08:42
  • Thanks for the reply and for pointing me to the diagram, that surely helps. – cana Mar 04 '13 at 08:53

0 Answers0