Skip to content Skip to sidebar Skip to footer

Php $xpath->query Expression Not Working

PHP xpath query not working. any idea? Problem # 1 HTML Source: Some contents i want to capture &

Solution 1:

Problem #1

Based on your markup, you're trying to target <td> tags, but in your query, it's //div, which doesn't make sense. Target <td>'s:

$rows = $xpath->query('//tr/td[@class = "abc pqr xyz"]');
foreach($rowsas$b){
    echo$b->nodeValue . '<br/>';
}

Sample Output

Problem #2

This is most likely related to this issue:

https://bugs.php.net/bug.php?id=61858&edit=1

Problem #3

You could just continue to use xpath to target the desired values. Select all those <td>'s and from there, just use each of them as the context node:

$data = array();
$td = $xpath->query('//td');
foreach($tdas$b){
    $data[] = array(
        'title' => $xpath->evaluate('string(./h3/a)', $b),
        'link' => $xpath->evaluate('string(./h3/a/@href)', $b),
        'small' => trim($xpath->evaluate('string(./h3/small)', $b)),
        'blahblah' => trim($xpath->evaluate('string(./div[@class="blahblah"])', $b)),
    );
}

Sample Output

Post a Comment for "Php $xpath->query Expression Not Working"