1

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great, sorry for the delay!
1

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']")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.