Reputation: 45
i'm having some troubles using Dom simple parser, I would like to get some values from a table in a html file, i want only values in td that has id='ok'.
I mean:
<tr>
<td id="no"> 18 </td>
<td id="yes"> 19 </td>
<td id="maybe"> 20 </td>
<td id="ok"> 21 </td> ---- i only want this value
<tr>
<tr>
<td id="no"> 18 </td>
<td id="yes"> 19 </td>
<td id="maybe"> 20 </td>
<td id="no"> 25 </td>
<tr>
i'm trying to use this code:
$ret = $html->find('td[id='ok']');
but it seems it doesn't work. Anyone has an idea?
Upvotes: 1
Views: 643
Reputation: 4114
One more solution (without third-party parsers) is to use DOMDocument
and XPATH
$doc = new DOMDocument();
// Making validator to be less strict (bec. invalid XML structure will cause parsing failure)
$doc->strictErrorChecking = false;
// Reading HTML directly in argument (saving one line of code)
$doc->loadHTML( file_get_contents('/some/test.html') );
$xml = simplexml_import_dom($doc);
// Applying XPATH on parsed document
$nodes = $xml->xpath("//*[@id='ok']")
Upvotes: 1
Reputation: 514
It should. Here's a different selector. Both worked for me.
require_once 'simple_html_dom.php';
$html = file_get_html('test.html');
$elem = $html->find('td#ok', 0);
echo $elem->plaintext;
Note: find() returns an array unless the 2nd parameter (index) is specified
Upvotes: 1