I have a string in variable $html
that contains minified HTMl code, all in one line, like:
$html = '<body><div><p>hello</p><div></body>';
How do I beautify/pretty print the HTML so that my variable becomes like:
$html = '<body>
<div>
<p>hello</p>
<div>
</body>';
I know the tidy extension is a possibility, but how can this be done without an extension.
EDIT: PLEASE read the question. I am not asking how to beautify HTML code via some external site. I am asking how to do it in PHP, specifically targeting the string variable.
To improve the formatting of your HTML source code, you can use the Format Document command Ctrl+Shift+I to format the entire file or Format Selection Ctrl+K Ctrl+F to just format the selected text. The HTML formatter is based on js-beautify.
The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe.
HTML Beautifier. Free tool to convert your minified html code into human readable and understandable format. You can copy the beautified code and use it in your application development. Input Browse... Output.
Using DomDocument
we load the html passing the LIBXML_HTML_NOIMPLIED
flag
which will prevent the loadHTML
method to add the extra html
wrapper.
We save as XML to get the nice indentation, while passing the $dom->documentElement
parameter to prevent the XML
header.
$html = '<body><div><p>hello</p><div></body>';
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($html,LIBXML_HTML_NOIMPLIED);
$dom->formatOutput = true;
print $dom->saveXML($dom->documentElement);
This will output
<body>
<div>
<p>hello</p>
<div/>
</div>
</body>
Notice that the HTML
was fixed for you as the second div
should have been a closing tag, I assume.
If we pass the proper HTML
as the input string, the output will be as you require
$html = '<body><div><p>hello</p></div></body>';
<body>
<div>
<p>hello</p>
</div>
</body>
I've used DOMDocument
but it seems that it is very sensitive to broken html and html errors.
Anyhow, DOMDocument
require dom extension
so I've used tidy php extension as it works perfect to me - it fix html errors and prettify html as well.
Use code from example:
$config = array(
'indent' => true,
'output-xhtml' => true,
'wrap' => 200);
// Tidy
$tidy = new \tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
// Output
echo $tidy;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With