0

I have this long list of courses on a html page done in tables i.e.

<tr class="navyLightgrey">
<td>3ADC7S1</td><td>SOFTWARE PROJECT MANAGEMENT (IIT Sri Lanka)</td><td align="Center">2009/0</td><td align="Center">Y</td><td align="Center">

<a id="dgModules__ctl2_lnkModule" href="http://example.com">View</a>

             </td>

            </tr>

<tr class="navyLightgrey">
<td>3ADC7S2</td><td>SOFTWARE ARCHITECTURE (IIT Sri Lanka)</td><td align="Center">2009/0</td><td align="Center">Y</td><td align="Center">

<a id="dgModules__ctl3_lnkModule" href="http://example.com" target="_self">View</a>

           </td>

             </tr>

It's done in this format. I want to get the value of the second td where it says the course name i.e. SOFTWARE ARCHITECTURE (IIT Sri Lanka) and SOFTWARE PROJECT MANAGEMENT (IIT Sri Lanka) for each tr item. I want to do a while loop through the html page and get each value and echo it. Thanks

Belinda
  • 1,230
  • 2
  • 14
  • 25
pingpong
  • 1,157
  • 4
  • 19
  • 32

3 Answers3

2
$html = 'your html';
$dom = new DOMDocument();
$dom->loadHTML($html); // or loadHTMLFile

$xpath = new DOMXPath($dom);
$arrNodes = $xpath->query('//tr/td[2]/text()');

foreach($arrNodes as $node)
    echo $node->nodeValue . '<br />';
johnlemon
  • 20,761
  • 42
  • 119
  • 178
1

Use DOMDocument::loadHTML, then search for the correct element within the DOMDocument.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
1

That is done with "HTML DOM Parser". You can use, for example this one.

Deele
  • 3,728
  • 2
  • 33
  • 51
  • thanks for this, when i find the in the html page, how can i tell it to access the second ? :)) +! from me – pingpong Mar 31 '11 at 10:02
  • for that is "foreach" loop, it will loop through all matched elements. If you want to access "next TR after TR, where is some identifying stuff", you should add some "if matched, set variable found=1" and add in loop "if found = 1 then this is ne one TR I need". – Deele Mar 31 '11 at 10:05