Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>using <a href="http://php.net/manual/en/book.tidy.php" rel="nofollow">Tidy</a>, <a href="http://www.php.net/manual/en/class.domdocument.php" rel="nofollow">DOMDocument</a> and <a href="http://www.php.net/manual/en/class.domxpath.php" rel="nofollow">DOMXPath</a> (make sure the PHP extensions are enabled) you can do something like this:</p> <pre><code>&lt;?php $url = "http://example.org/test.html"; function get_data_from_table($id, $url) { // retrieve the content of that url $content = file_get_contents($url); // repair bad HTML $tidy = tidy_parse_string($content); $tidy-&gt;cleanRepair(); $content = (string)$tidy; // load into DOM $dom = new DOMDocument(); $dom-&gt;loadHTML($content); // make xpath-able $xpath = new DOMXPath($dom); // search for the first td of each tr, where its content is $id $query = "//tr/td[position()=1 and normalize-space(text())='$id']"; $elements = $xpath-&gt;query($query); if ($elements-&gt;length != 1) { // not exactly 1 result as expected? return number of hits return $elements-&gt;length; } // our td was found $element = $elements-&gt;item(0); // get his parent element (tr) $tr = $element-&gt;parentNode; $data = array(); // iterate over it's td elements foreach ($tr-&gt;getElementsByTagName("td") as $td) { // retrieve the content as text $data[] = $td-&gt;textContent; } // return the array of &lt;td&gt; contents return $data; } echo '&lt;pre&gt;'; print_r( get_data_from_table( 414, $url ) ); echo '&lt;/pre&gt;'; </code></pre> <p>Your HTML source (http://example.org/test.html):</p> <pre><code>&lt;table&gt;&lt;tr&gt; &lt;td&gt;413&lt;/td&gt; &lt;td&gt;Party Hat&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;No&lt;/td&gt; &lt;td&gt;&lt;a href="http://clubpenguincheatsnow.com/tools/swfviewer/items.swf?id=413"&gt;View SWF&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;414&lt;/td&gt; &lt;td&gt;Party Hat&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;No&lt;/td&gt; &lt;td&gt;&lt;a href="http://clubpenguincheatsnow.com/tools/swfviewer/items.swf?id=413"&gt;View SWF&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; </code></pre> <p>(as you can see, no valid HTML, but this doesn't matter)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload