Get values ​​from each td

I use phpQuery to get data from elements.

I try to get values ​​from the first td, seconds tdand hreflinks from each tr.

<table>
  <tr class="A2"> 
    <td> Text 1 </td>
    <td> Text 2 </td>
    <td> Text 3 </td>
    <td> <a href="linkhere1">  Text 131</a> </td>
  </tr>
  <tr class="A2"> 
    <td> Text 4 </td>
    <td> Text 5 </td>
    <td> Text 6 </td>
    <td> <a href="linkhere2">  Text 123213</a> </td>
  </tr>
  <tr class="A2"> 
    <td> Text 7 </td>
    <td> Text 8 </td>
    <td> Text 9 </td>
    <td> <a href="linkhere3.php">  Text 213213 </a> </td>
  </tr>
</table>

How to do it? I tried:

<?
require('phpQuery.php');

$file = file_get_contents('test.txt', true);

$html = phpQuery::newDocument($file);

foreach($html->find('.A2')  as $tag) {                                           
  echo pq('td'); // problem here?
}
?>
+3
source share
2 answers

I think you switched them ..

foreach(pq('.A2') as $tag) {
   $tds = pq($tag)->find('td');
}

To get the value from each td, you can iterate over it inside:

foreach(pq('.A2') as $tag) {
   foreach(pq($tag)->find('td') as $td) {
      // stuff
   }
}
+9
source

pq()will return a list of matching nodes (tags <td>in this case). You should iterate over this list:

foreach(pq('td') as $td) {
   ... do something ...
}
+2
source

All Articles