Prevent tidy from adding html tags
Asked Answered
P

1

19

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.

Pragmatics answered 26/12, 2009 at 12:43 Comment(0)
T
39

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.

Trembles answered 26/12, 2009 at 14:24 Comment(4)
Can't believe I missed that option. Thanks!!Pragmatics
Dear lord. This is exactly what I needed, but I never would have guessed it from the name of the option or even its description!Cooee
what if i want to load HTML but display it by XML? output-xml=true so show-body-only doesn't work. how to do in that case?Bio
@ПавелИванов, output-xml: true overrides show-body-only: yes/auto. I don't think there's any way to prevent this behavior.Aspirate

© 2022 - 2024 — McMap. All rights reserved.