📜  PHP | DOMDocument loadHTML()函数

📅  最后修改于: 2022-05-13 01:56:48.328000             🧑  作者: Mango

PHP | DOMDocument loadHTML()函数

DOMDocument::loadHTML()函数是PHP中的一个内置函数,用于从字符串加载 HTML 文件。

句法:

bool DOMDocument::loadHTML( string $source, int $options = 0 )

参数:该函数接受上面提到的两个参数,如下所述:

  • $source:此参数保存 HTML字符串。
  • $options:此参数用于指定PHP 5.4.0 和 Libxml 2.6.0 中的附加 Libxml 参数。

返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。如果静态调用此函数或失败时返回 FALSE,则此函数返回 DOMDocument。

错误/异常:如果将空字符串作为参数传递,则会生成警告消息。此函数也可以静态调用,但会发出 E_STRICT 错误。

下面的程序说明了PHP中的 DOMDocument::loadHTML()函数:

方案一:

loadHTML(
"

    
        DOMDocument::loadHTML() function
    


    

GeeksforGeeks

    

DOMDocument::loadHTML() function

     ");    // Creates an HTML document and display it echo $doc->saveHTML();    ?>
输出:



    
        DOMDocument::loadHTML() function
    


    

GeeksforGeeks

DOMDocument::loadHTML() function

方案二:

createComment('Starting of HTML document file');
  
// Append element to the document
$doc->appendChild($comm1);
  
// Creates an HTML document and display it
echo $doc->saveHTML();
  
// Load the HTML element to the document
$doc->loadHTML(
"

    PHP function


    

Welcome to GeeksforGeeks

    

PHP function

    
A computer science portal
     ");    // Create an element $comm2 = $doc->createComment('Ending of HTML document file');    // Append element to the document $doc->appendChild($comm2);    // Creates an HTML document and display it echo $doc->saveHTML();    ?>
输出:




    PHP function


    

Welcome to GeeksforGeeks

PHP function

A computer science portal

参考: https://www. PHP.net/manual/en/domdocument.loadhtml。 PHP