代码之家  ›  专栏  ›  技术社区  ›  ufk

PHP SimpleXML:如何加载HTML文件?

  •  3
  • ufk  · 技术社区  · 15 年前

    当我尝试使用 simplexml_load_string

    这个HTML文件可能有不需要的空格,可能还有一些我希望SimpleXML忽略的错误。

    4 回复  |  直到 12 年前
        1
  •  3
  •   robjmills    15 年前

    我建议使用 PHP Simple HTML DOM . 我自己也用过它,从页面抓取到操作HTML模板文件,它非常简单而且非常强大,应该可以很好地满足您的需求。

    以下是他们文档中的几个例子,展示了您可以做的事情:

    // Create DOM from URL or file
    $html = file_get_html('http://www.google.com/');
    
    // Find all images
    foreach($html->find('img') as $element)
           echo $element->src . '<br>';
    
    // Find all links
    foreach($html->find('a') as $element)
           echo $element->href . '<br>'; 
    
        2
  •  20
  •   cweiske agentofuser    12 年前

    使用 DomDocument::loadHtmlFile simplexml_import_dom 将非格式良好的HTML页面加载到SimpleXML中。

        3
  •  1
  •   Leif Wright    8 年前

        //suppresses errors generated by poorly-formed xml
        libxml_use_internal_errors(true);
    
        //create the html object
        $html = new DOMDocument();
    
        //load the external html file
        $html->loadHtmlFile('http://blahwhatever.com/');
    
        //import the HTML object into simple xml
        $shtml = simplexml_import_dom($html);
    
        //print the result
        echo "<pre>";
        print_r($shtml);
        echo "</pre>";
    
        4
  •  0
  •   Sergey Eremin    15 年前

    检查 this