Can't Load And Parse DOM Object

后端 未结 2 1551
甜味超标
甜味超标 2021-01-25 01:16

I am trying to load a dom object and to parse it for finding specific links
Here is my code:

$content = file_get_contents(\"http://www.example.com/example.         


        
相关标签:
2条回答
  • 2021-01-25 01:39

    Instead of var_dump($xml) try: echo $xml->saveHTML();

    0 讨论(0)
  • 2021-01-25 01:40

    This is an example from the manual: DOMDocument

    <?php 
    $content = file_get_contents("http://www.example.com/example.php");
    $xml = new DOMDocument();
    $xml->loadHTML($content);
    
    // Empty array to hold all links to return
    $links = array();
    
    //Loop through each <a> tag in the dom and add it to the link array
    foreach($xml->getElementsByTagName('a') as $link) {
        $links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
    }
    
    print_r($links);
    ?>
    
    0 讨论(0)
提交回复
热议问题