17

我有一个生成一些 html(表单元素和表格元素)的类,但是这个类在一行中返回所有 html。

所以我试图使用 tidy 来美化代码(缩进代码,放置换行符等),我遇到的唯一问题是它也会生成我不想要的标签。

这是代码:

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

我发现删除额外 html 标记的唯一方法是添加 str_replace,如下所示:

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

哪个有效,但我真的很希望有一种方法可以告诉 tidy 只是美化代码而不插入额外的代码。

4

1 回答 1

38

试试show-body-only选项。

例如

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

印刷

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

(字符串已被修复和缩进,但没有添加 html/body 包装器)。可以与output-xhtml选项结合使用,在这种情况下也会为空输入元素添加斜线。

于 2009-12-26T14:24:57.717 回答