Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautify HTML stored in a string on PHP

Tags:

html

php

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.

like image 776
Henrik Petterson Avatar asked Jan 01 '16 13:01

Henrik Petterson


People also ask

How do I beautify HTML code?

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.

How do I strip HTML tags in PHP?

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.

What is HTML beautifier?

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.


2 Answers

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>
like image 163
Alex Andrei Avatar answered Nov 03 '22 01:11

Alex Andrei


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;
like image 36
Serhii Polishchuk Avatar answered Nov 03 '22 00:11

Serhii Polishchuk