PHP | Função DOMDocument saveHTMLFile()
A função DOMDocument::saveHTMLFile() é uma função embutida no PHP que é usada para criar um documento HTML a partir da representação DOM. Esta função é usada após a criação do documento dom.
Sintaxe:
int DOMDocument::saveHTMLFile( string $filename )
Parâmetros: Esta função aceita um único parâmetro $filename que contém o caminho para salvar o documento HTML.
Valor de retorno: esta função retorna o número de bytes em caso de sucesso ou FALSE em caso de falha.
O programa abaixo ilustra a função DOMDocument::saveHTMLFile() em PHP:
Programa:
<?php
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0');
// Create a root element
$root = $domDocument->createElement('html');
// Append the element to the document as root element
$root = $domDocument->appendChild($root);
// Create a head element
$head = $domDocument->createElement('head');
// Append the element to the document
// as child element
$head = $root->appendChild($head);
// Create a title element
$title = $domDocument->createElement('title');
// Append the element to the document
// as child element
$title = $head->appendChild($title);
// Create a text node
$text = $domDocument->createTextNode(
'DOMDocument::saveHTML() function');
// Add the text node the the title element
$text = $title->appendChild($text);
// Create a body element
$body = $domDocument->createElement('body');
// Append the element to the document
// as child element
$body = $root->appendChild($body);
// Create a heading element
$h1 = $domDocument->createElement('h1');
// Append the element to the document
$h1 = $body->appendChild($h1);
// Create a text node
$text = $domDocument->createTextNode('GeeksforGeeks');
// Add the text node to the heading element
$text = $h1->appendChild($text);
// Create a heading element
$h2 = $domDocument->createElement('h2');
// Append the element to the document
$h2 = $body->appendChild($h2);
// Create a text node
$text = $domDocument->createTextNode(
'DOMDocument::saveHTML() function');
// Add the text node to the heading element
$text = $h2->appendChild($text);
// Use saveHTMLFile() function to save
// an HTML document
$domDocument->saveHTMLFile('gfg.html');
echo "HTML file saved successfully";
?>
Saída:
HTML file saved successfully
O conteúdo do arquivo HTML salvo gfg.html:
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>DOMDocument::saveHTML() function</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>DOMDocument::saveHTML() function</h2>
</body>
</html>
Referência: https://www.php.net/manual/en/domdocument.savehtmlfile.php
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva