How to insert HTML using PHP in indented form

Asked

Viewed 187 times

-4

I created a PHP file that when executed CREATES an html file already with an html code inside it. Later this PHP file that is being executed TAKES this HTML file and inserts another html code in a specific html div created. So far blz I can already do, however I needed the final INDENTED file for later use.

And there is the problem I can not be free to indent this code, I say this because I put space, break line, use n and nothing is obeyed... I’m starting PHP so I already apologize for any ignorance hehe...

Preferably I wanted to see if I could solve this without the help of plugins, I really wanted to be able to do it in a "manual"...

I’ll try to exemplify what I did:

<?php

    $arquivoHtml = 'pasta/index.html';

    $contentHtml = file_get_contents('default.html');
    $fileHtml = fopen($arquivoHtml,'wb');
    fwrite($fileHtml,$contentHtml);
    fclose($fileHtml);

    $html = file_get_contents($arquivoHtml);

    $dom  = new DomDocument();
    $dom -> loadHTML($html);

    $node = $dom -> getElementsByTagName('div')->item(0);

    $fragmento = $dom -> createDocumentFragment();
    $fragmento -> appendXML('
        <div id="id-da-div">
        </div>
    ');

    $node -> appendChild($fragmento);
    echo $dom -> saveHTMLFile($arquivoHtml);

?>

The result is exactly this hehe tortinho code:

<!DOCTYPE html>
<html lang="pt-br"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Título</title></head><body>
    <div id="main">


                <div id="id-da-div">
                </div>

</div>
</body></html>

I even tried to put something of the kind to complement:

$dom -> formatOutput       = true;
$dom -> preserveWhiteSpace = true;

With that some things were right but not totally, it’s still very strange.

  • The way you insert it into PHP will look. Just indent the strings and snippets run the way you want. Important [Dit] your post and explain where you had difficulty with it, since it usually works the way you typed it. Example: echo '   <div>'; - this will have three spaces in the beginning, because I inserted inside the ' '. Alternatively, you can do it with double quotes and escape: echo "\t\t\t<div>\n"; - t to tab and n to line break. But there are a thousand other ways to do it, the hard part is NOT getting it :) It tested with real code and it gave problem, edit with a [mcve].

  • Thank you very much for the reply hehe, well, as you advised me I edited my question to maybe better present she is a little new in the area and in php I also apologize for the "leiguice" hehehe

  • Now, looking at your code, it seems to me that the problem is that you are mixing the indentation of PHP with that of HTML, there really will not coincide in most situations. Ideally you should see how many spaces or tabs you need to put in HTML, and make exactly the same amount in the append. Honestly, if you want to have more control, maybe Domdocument is not the best way. (besides the traditional echo is more efficient)

  • Thank you for the return partner! I could give an echo, put on an external page?

  • Alias and I could then use something other than the new Domdocument() ?

1 answer

2

You can use Heredoc

But the recommended is that you create a separate php file with html and include it Heredoc example:

print <<< END
<p>
Seu código HTML<br/>
</p>
END;

Include example:

<?php
//your code
include 'template-index.php';
?>

Take a look at some template engine, like the Smarty

As the question was edited for a question about Domdocument, try using it this way

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadHTML($html);
$dom->save($arquivoHtml);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.