koff75
koff75

Reputation: 99

Filter with xPath

I would like to parse a webpage, but i have a trouble :

I have in final this error :

Parse error: syntax error, unexpected 'Steve' (T_STRING)

<?php    
// Here I would like to parse a wikipedia page    
        $url = "http://fr.wikipedia.org/wiki/Jobs_(film)"; 
        $html = file_get_contents($url);  
        $doc = new DOMDocument();
        $doc->loadHTML($html); 
// I'm use xPath for parsing page
        $xpath = new DOMXpath($doc);
    // Here I save all links of the wikipedia page
        $nodes = $xpath->query('//a');
        ?>

    <?php
    // Showing elements :
    if($nodes)
    {

    echo '<h1>les <span class="red">'.$nodes->length. '</span> liens de la page : '.$url.'</h1>';
    // Table to show some elements
        echo '<table>
                <thead><tr><th>ancre</th><th>title</th><th>url</th><th>rel</th></tr></thead><tbody>';
    // Here I search all the elements with title, links...
        foreach($nodes as $node) {
            if($node->getAttribute('rel')){$rel = $node->getAttribute('rel');}else{$rel= "-";}
            if($node->getAttribute('title')){$title = $node->getAttribute('title');}else{$title= "-";}
            if($node->nodeValue){$ancre = $node->nodeValue;}else{$rel= "-";}
            if($node->getAttribute('href[contains(text(),'Steve')]')){$href = $node->getAttribute('href[contains(text(),'Steve')]');}else{$rel= "-";}
            // The table contains all element but i would like a filter...
            echo '<tr><td>'. $ancre .'</td><td>'. $title .'</td><td>'. $href .'</td><td>'.$rel.'</td></tr>';    
        }

        echo '</tbody></table>';
    }

Upvotes: 0

Views: 167

Answers (3)

lafor
lafor

Reputation: 12776

You have single quotes inside single quotes in this line:

if($node->getAttribute('href[contains(text(),'Steve')]'))

Either escape them (\') or replace with double quotes (").

Upvotes: 1

Kamehameha
Kamehameha

Reputation: 5488

You got the quotes wrong. -

if($node->getAttribute("href[contains(text(),'Steve')]")){$href = $node->getAttribute("href[contains(text(),'Steve')]");}else{$rel= "-";}
                       ^                              ^                               ^                              ^

Upvotes: 1

Nouphal.M
Nouphal.M

Reputation: 6344

its a quotes problem

if($node->getAttribute("href[contains(text(),'Steve')]")){$href = $node->getAttribute("href[contains(text(),'Steve')]");}else{$rel= "-";}

Upvotes: 1

Related Questions