tags:

views:

63

answers:

2

Hello guys i am trying to get the text inside specific <td's>. Each one has a id="uniqueName" - Such as: <td id="engineTypeField" class="normal2" colspan="2"> 3.0L L6 SFI DOHC</td> but i am having bad luck - learning regexp as doing so

This is what i have: preg_match ("<td id='\/engineTypeField\/(.*)>(.*)</td>i", $result, $matches); // testing just enginetype

A: 

nevermind it pasted my Q all wrong

Haso KEric
+3  A: 

Don't use regular expressions to parse HTML. Use DOMDocument instead.

e.g.

<?php
$dom = new DOMDocument();
$dom->loadHTML('<td id="engineTypeField" class="normal2" colspan="2"> 3.0L L6 SFI DOHC</td>');

foreach ($dom->getElementsByTagName('td') as $td) {
    if ($td->getAttribute('id') == 'engineTypeField') {
        echo $td->textContent.PHP_EOL;
    }
}

Output:

 3.0L L6 SFI DOHC
Ben James