title: Targeting specific "nth" HTML tags with PHP Simple HTML DOM Parser
body: <p>I am using the PHP Simple HTML DOM Parser (<a href="http://simplehtmldom.sourceforge.net/" rel="nofollow">http://simplehtmldom.sourceforge.net/</a>) to read through a website and output particular information.  </p>
  
  <p>I'm trying to output the contents of specific ,tr, tags in every table, and the contents of specific ,p, tags, rather than all tables and all paragraphs.</p>
  
  <p>Therefore, Ideally I would like to set up some PHP code that involves numeric parameters which refer target specific "nth" ,td, or ,p, tags.</p>
  
  <hr>
  
  <p>As a PHP novice, I greatly appreciate the expertise that is found on StackOverflow.
  Thank you for your time and assistance in figuring out my questions.</p>
  
  <p>The first question set is here, above the code.  The second question set can be found at the bottom of this post, with the PHP code.</p>
  
  <hr>
  
  <p>1st question set:</p>
  
  <p>A. How does one output the 2nd and 3rd  of every table?</p>
  
  <p>AND</p>
  
  <p>B. How does one output the 4th paragraph after every table and exclude the ,a, tag it contains?</p>
  
  <p>IN</p>
  
  <p>The following HTML code</p>
  
  <p>USING</p>
  
  <p>The PHP Simple HTML DOM Parser as shown in the following PHP code</p>
  
  <p>UNLESS</p>
  
  <p>You have a different suggestion that you believe is better</p>
  
  <hr>
  
  <p>Below is sample HTML code followed by PHP code and another relevant question set.  </p>
  
  <p>This is the main HTML I am interested in.</p>
  
  <pre><code>&lt;a name=“arbitrary_a_tag_Begin_Item_01”&gt;&lt;/a&gt;
  &lt;h2&gt;Item No. 1 &lt;/h2&gt;
  
  &lt;table&gt;
  
  &lt;tbody&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Item Description:&lt;/td&gt;
  &lt;td&gt;Big blue ball&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Property Location:&lt;/td&gt;
  &lt;td&gt;Storage Closet&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Owner:&lt;/td&gt;
  &lt;td&gt;Gym&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Cost&lt;/td&gt;
  &lt;td&gt;20.00&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Vendor:&lt;/td&gt;
  &lt;td&gt;Jim’s Gym Toys&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;p&gt;
  Approximate minimum acceptable grage sale price: $10
  &lt;br&gt;
  6 month redemption period
  &lt;/p&gt;
  
  &lt;p&gt;
  &lt;img src="../dec/Item01.jpg"&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
  &lt;a target="new" href="http://pictures/Item01.jpg”&gt;Picture of Item 01&lt;/a&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
  Current status: In Stock
  &lt;a name=“arbitrary_a_tag_Begin_Item_02&gt;&lt;/a&gt;
  &lt;/p&gt;
  
  
  &lt;h2&gt;Item No. 2 &lt;/h2&gt;
  
  &lt;table&gt;
  
  &lt;tbody&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Item Description:&lt;/td&gt;
  &lt;td&gt;Green tennis racket&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Property Location:&lt;/td&gt;
  &lt;td&gt;Gear Lockers&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Owner:&lt;/td&gt;
  &lt;td&gt;Tennis Team&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Cost&lt;/td&gt;
  &lt;td&gt;50.00&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Vendor:&lt;/td&gt;
  &lt;td&gt;Jim’s Gym Toys&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;p&gt;
  Approximate minimum acceptable grage sale price: $25
  &lt;br&gt;
  6 month redemption period
  &lt;/p&gt;
  
  &lt;p&gt;
  &lt;img src="../dec/Item02.jpg"&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
  &lt;a target="new" href="http://pictures/Item02.jpg”&gt;Picture of Item 02&lt;/a&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
  Current status: In Stock
  &lt;a name=“arbitrary_a_tag_Begin_Item_03&gt;&lt;/a&gt;
  &lt;/p&gt;
  
  
  &lt;h2&gt;Item No. 3 &lt;/h2&gt;
  
  &lt;table&gt;
  
  &lt;tbody&gt;
  
  &lt;tr&gt;
  &lt;td&gt;Item Description:&lt;/td&gt;
  &lt;td&gt;Red Soccer Ball&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>Etc. etc. etc.</p>
  
  <p>The PHP code USING "PHP Simple HTML DOM Parser":</p>
  
  <pre><code>&lt;?php
  // Include the library
  include('simple_html_dom.php');
  
  $url = 'http://www.URL.com';
  
  // Create DOM from URL or file
  $html = file_get_html($url);
  
  foreach($html-&gt;find('table') as $table) 
  {
  echo '&lt;table&gt;&lt;tbody&gt;';
  foreach($table-&gt;find('tr') as $tr) 
  {   
  echo '&lt;tr&gt;';
  foreach($tr-&gt;find('td') as $td)  
  {   
  echo '&lt;td&gt;';
  echo $td-&gt;innertext;
  echo '&lt;/td&gt;';
  }
  echo '&lt;/tr&gt;';
  }
  echo '&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;';
  }
  </code></pre>
  
  <hr>
  
  <p>Some things I have come across and unsuccessfully attempted to implement to access specific tags:</p>
  
  <hr>
  
  <p>The First Concept</p>
  
  <pre><code>$e = $html-&gt;find('table', 0)-&gt;find('tr', 1)-&gt;find('td');
  foreach($e as $d){
  echo $d;
  }
  </code></pre>
  
  <p>Second concept:</p>
  
  <pre><code>$file = file_get_contents($url);
  preg_match_all('#&lt;p&gt;([^&lt;]*)&lt;/p&gt;#Usi', $file, $matches);
  foreach ($matches as $match)
  {
  echo $match;
  }
  </code></pre>
  
  <hr>
  
  <p>Second Question Set:</p>
  
  <p>Regarding this first concept above,
  How do I set up a while loop to iterate through, lets say 12 tables?
  For example, this: $e = $html->find('table', 0)
  reads only the first table.
  Yet, I am not sure how to replace the 0 with a variable, such as $i, which can be autoincremented.</p>
  
  <pre><code>$i = 1;
  while($i&lt;=12){
  What goes here??
  }
  $i++
  </code></pre>
  
  <p>Regarding the second concept,
  How can I use this (or the first concept) to: </p>
  
  <ol>
  <li><p>Return an array of all p tags after each table</p></li>
  <li><p>Read through the string contents (the "contents") within each p tag, and check it against string (the "key")</p></li>
  <li><p>Only return the string "contents" when the key string is found within the contents</p></li>
  <li><p>Before outputting the returned "contents" featuring the matched string, exclude/remove a 2nd matched string from the information to be output  (for example, in the 1st Question Set, I want to grab everything within a specific ,p, tag, but exclude everything within the ,a, tag).</p></li>
  </ol>
  
  <hr>
  
  <p>Thanks very much for your time and assistance!</p>