Question

<tr>
<td>New order info</td>
<td class="emailid"><input type="button" class="product product-info" value="View product" onclick="popupWindow('viewproduct.php?id=481244','emlmsg',650,400)" /></td>
</tr>
 <tr

i want to get the id number in the td tag preceded by 'New order info'. above is an excerpt of the html code. i tried to do this using both regex and domdocument but cann't get the desired result. i'm thinking about getting all td tags elements using DocDocument's getElementsByTagName method, and if the td text Value is 'New order info',get the attributes in the next td tag.but i'm not sure how to do this or this is the right way.i tried nextSibling but not working in this case. are there any way to get the attributes value in the next td tag?

$DOMNodelist = $doc->getElementsByTagName('td');
                  foreach($DOMNodelist as $DOMElements) {

                       if ($DOMElements->nodeValue == "New order info") {

                                      ...................
                         }

                    }

Thank you very much!

Was it helpful?

Solution

Use XPath here:

$html = <<<EOF
<tr>
<td>New order info</td>
<td class="emailid"><input type="button" class="product product-info" value="View product" onclick="popupWindow('viewproduct.php?id=481244','emlmsg',650,400)" /></td>
</tr>
EOF;

$doc = new DOMDocument();
$doc->loadHTML($html);
$selector = new DOMXPath($doc);

$td = $selector->query('//td[text() = "New order info"]/following-sibling::td')->item(0);
var_dump($td);

The example above selects the <td> node preceded by 'New order info'. However, the td tag has no id attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top