Domdocument Parse Html
I have one html page where there are number of elements like address: Copy
This finds any <td> node that has a text equal to "address:", grabs the following <td>, goes into the <b> inside it and gets you the text it finds there.
That means you can do
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
echo$xpath->evaluate('string(//td[text()="address:"]/following-sibling::td/b)');
It will immediately output the result you are looking for.
Solution 2:
You have to get the <tr> elements, then parse its children, similar to:
$trElements = $doc->getElementsByTagName("tr");
foreach ($trElementsas$node) {
$children = $node->childNodes;
foreach( $childrenas$child)
echo$child->textContent; // or $child->nodeValue
}
This outputs: address: 12284,CA
Now, if there are more <tr> elements that are not the address, you will need to parse the $children list of nodes to make sure you find "address:", and then once you do, you know the value of next child is the value you're looking for.
Solution 3:
I got the answer by myself which is similar to nickb's answer
$tdElements = $doc->getElementsByTagName("td");
$tdCnt = $tdElements->length;
for ($idx = 0; $idx < $tdCnt; $idx++) {
if(trim($tdElements->item($idx)->nodeValue) == 'address:'){
echo$tdElements->item($idx+1)->nodeValue;
}
}
Hope it will helps
Post a Comment for "Domdocument Parse Html"