Prevent tidy from adding html tags

倾然丶 夕夏残阳落幕 提交于 2019-11-29 02:34:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1963298/prevent-tidy-from-adding-html-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!