Idro
Idro

Reputation: 349

Trying to extract infos from an html page with PHP

I'd like to know how to do the following. I have an html page with this part of code:

<div id="sportSelect" class="longSelect">
                <select name="ctl00$ContentPlaceViewResults$MedalistResultsSearchSmallBlock$ResultsSearchSmallBlock$SportsListing" id="ctl00_ContentPlaceViewResults_MedalistResultsSearchSmallBlock_ResultsSearchSmallBlock_SportsListing" class="dropdownfilter">
        <option value="">Choose Sport*</option>
        <option selected="selected" value="31766">Archery</option>
        <option value="32588">Athletics</option>
        <option value="31753">Badminton</option>
        <option value="126990">Baseball</option>
        <option value="31341">Basketball</option>
</div>

Now I implemented this code:

<html>

<body>

<?php
// Include the library
include('simple_html_dom.php');

$html=file_get_html('http://www.olympic.org/olympic-results/london-2012/archery');
foreach($html->find('div') as $element) {
    if ($element->id == 'sportSelect')
    {
        $v =  $element->find('option[selected]', 0)->value . '</br>'; 
    }
}

echo $v

?>
</body>
</html>

This is ok, it works because I get the value 31766 (the code corrisponding to Archery). Actually I would want to get "Archery" in the v variable and not 31766.

Upvotes: 0

Views: 33

Answers (1)

Mat
Mat

Reputation: 2154

What I see from http://simplehtmldom.sourceforge.net/manual.htm is that you should use something similar to $element->find('option[selected]', 0)->plaintext to get the node's text content instead of its value attribute.

The best tip I can give you when discovering a new language / library is to read the documentation.

Upvotes: 1

Related Questions