Prevent tidy from adding html tags

AntonioCS picture AntonioCS · Dec 26, 2009 · Viewed 7.5k times · Source

I have a class that generates some html (form elements and table elements), but this class returns all the html in one line.

So I am trying to use tidy to beautify the code (indent the code, put line breaks, etc), the only problem I am having is that's also generating the tags I don't want.

Here is the code:

tidy_parse_string(
                    $table->getHtml(),
                    array(
                            'DocType' => 'omit',
                            'indent' => true,
                            'indent-spaces' => 4,                                    
                            'wrap' => 0                                    
                        )
                );

The only way I have found to remove the extra html tags is by adding a str_replace, something like this:

str_replace(array('<html>','</html>','<body>','</body>','<head>','</head>','<title>','</title>'),'', code);

Which works, but I was really hopping there would be a way to tell tidy to just beautify the code and not insert the extra code.

Answer

VolkerK picture VolkerK · Dec 26, 2009

Try the show-body-only option.

e.g.

$s = '<form method="post" action="?"><table><tr><td><input tpye="submit"></table>';
echo tidy_parse_string($s, array('show-body-only'=>true, 'indent'=>true));

prints

<form method="post" action="?">
  <table>
    <tr>
      <td>
        <input tpye="submit">
      </td>
    </tr>
  </table>
</form>

(string has been repaired and indented but no html/body wrapper added). Can be combined with the output-xhtml option which in this case would also add the slash for the empty input element.