Skip to content

Instantly share code, notes, and snippets.

@RundesBalli
Last active January 17, 2024 21:25
Show Gist options
  • Save RundesBalli/a5d20a8c92a9a004803980654e638cbb to your computer and use it in GitHub Desktop.
Save RundesBalli/a5d20a8c92a9a004803980654e638cbb to your computer and use it in GitHub Desktop.
PHP-Snippet: Tidy HTML Output

Small snippet for the PHP-Tidy function

Dependencies:

sudo apt install php-tidy

Example

turns this:

<html><body><p>your HTML code here</p></body></html>

into this:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
  </head>
  <body>
    <p>
      your HTML code here
    </p>
  </body>
</html>
<?php
$output = "<html><body><p>your HTML code here</p></body></html>";
/**
* @see https://api.html-tidy.org/tidy/quickref_5.6.0.html
*/
$tidyOptions = array(
'indent' => TRUE,
'output-xhtml' => TRUE,
'wrap' => 200,
'newline' => 'LF', /* LF = \n */
'output-encoding' => 'utf8',
'drop-empty-elements' => FALSE /* e.g. for placeholders */
);
$tidy = tidy_parse_string($output, $tidyOptions, 'UTF8');
tidy_clean_repair($tidy);
echo $tidy;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment