配置 HTML Tidy 以在 html 属性内忽略 PHP 短开始和结束标记
当用作 html 属性中的值时,如何防止 HTML Tidy 转换 PHP 短标签?
这是它当前所做的一个示例。 它将这个: 转换
<input value='<?=$variable?>'>
为:
<input value='<?=$variable?>'>
我希望 HTML Tidy 忽略 PHP 短标签。 有什么配置选项可以改变这个吗?
==
为了简化,有没有办法让 HTML Tidy 避免进行 HTML 实体转换? 如果它只是不转换< 和 >,这将解决我的问题。
How can I keep HTML Tidy from converting PHP short tags when used as values in html attributes?
Here's an example of what it currently does. It converts this:
<input value='<?=$variable?>'>
to this:
<input value='<?=$variable?>'>
I want HTML Tidy to ignore PHP short tags. Any config options that change this?
==
To simplify, is there a way to have HTML Tidy just avoid doing HTML entity conversion? If it would just not convert < and >, that would solve my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以预处理您的 php:
通过将
转换为
将变为
运行 HTMLtidy 后,您将执行相反的操作。
You could pre-process your php:
by adding comments by converting
<?
to<!--<?
, and?>
to?>-->
<input value='<?=$variable?>'>
would become
<input value='<!--<?=$variable?>-->'>
after running HTMLtidy, you would do the opposite.
一种快速方法是将所有
替换为
A quick way is to just replace all
<?=
with<?php echo
即使它可以完成,
Tidy
也不能很好地与php
配合使用 - 如果你的php
代码包含引号等,它可能会被阻塞。如果php
输出缺少属性,Tidy
可能会抛出有关缺失属性的警告。您可以在运行
Tidy
之前将所有替换为
<#$variable#>
,然后再将它们替换回来。在控制台中,例如这样:
第一行将
转换为
<#$variable#>
inside file,第二个运行整齐并更新文件,
第三行将 <#$variable#> 转换为 返回
。
Even if it could be done
Tidy
doesn't work too well withphp
- it might choke if yourphp
code contains quote marks etc. AlsoTidy
might throw warnings about missing attributes if they're output byphp
.You could replace all
<?$variable?>
to<#$variable#>
before runningTidy
and then replace them back.In the console, for example like this:
The first line converts
<?=$variable?>
to<#$variable#>
inside file,the second runs tidy in place and updates the file,
The third line converts <#$variable#> back to
<?=$variable>
.未处理的 PHP 与 HTML 不兼容(除了少数最琐碎的情况),Tidy 仅对其提供表面支持。
实际上,Tidy 可能以无数种方式导致文档中的错误,因为它无法理解 PHP 生成的内容以及它如何与其他标记交互。
为了获得正确且可靠的结果,您应该对纯 HTML 输出进行后处理。 您可以通过在 PHP 中添加 Tidy 过滤器来做到这一点:
这会影响运行时性能,但对于大多数站点来说这不是问题,因为 Tidy 相当快。
Unprocessed PHP is not compatible with HTML (except few most trivial cases), and Tidy has only superficial support for it.
There are countless ways in which Tidy can actually cause errors in the document, because it won't understand what PHP generates and how it interacts with other markup.
To get correct and reliable results you should postprocess HTML-only output. You can do that by adding Tidy filter in PHP:
This will impact runtime performance, but it's not a problem for most sites as Tidy is quite fast.