Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent tidy from adding html tags

Tags:

html

forms

php

tidy

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.

like image 280
AntonioCS Avatar asked Dec 26 '09 12:12

AntonioCS


1 Answers

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.

like image 142
VolkerK Avatar answered Oct 05 '22 12:10

VolkerK