防止添加html标签的整洁 [英] Prevent tidy from adding html tags

查看:149
本文介绍了防止添加html标签的整洁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

使用整洁来美化代码(缩进代码,放置换行符等),唯一的问题是我也在生成我不想要的标记。



以下是代码:

  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>')。 

哪些方法可行,但我确实在跳跃,有一种方法可以让整理代码美化而不是插入额外的代码。 试试仅限展示机构选项。

eg

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

打印

 < form method =postaction =?> 
< table>
< tr>
< td>
< input tpye =submit>
< / td>
< / tr>
< / table>
< / form>

(字符串已被修复并缩进,但未添加任何html / body封装)。
可以与 output-xhtml 选项结合使用,其中这种情况下也会为空输入元素添加斜杠。

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.

解决方案

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.

这篇关于防止添加html标签的整洁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆