I have a html:
$content = "
<tr>
<td class="ttl"><a href="#">Colors</a></td>
<td class="nfo">Dark Grey, Rose Red, Blue, Brown, Sand White</td>
</tr>";
And code php:
$dom = new DOMDocument();
@$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$attbs = $xpath->query("//td[@class='ttl']");
foreach($attbs as $a) {
print $a->nodeValue;
}
$values = $xpath->query("//td[@class='nfo']");
foreach($values as $v) {
print $v->nodeValue;
}
How get value of 2 td but only using 1 foreach
Put both classes into a logical OR expression with the | operator:
$attbs = $xpath->query("//td[@class='ttl'] | //td[@class='nfo']");
foreach($attbs as $a) {
print $a->nodeValue;
}
This prints:
ColorsDark Grey, Rose Red, Blue, Brown, Sand White
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With