配置 HTML Tidy 以在 html 属性内忽略 PHP 短开始和结束标记

发布于 2024-08-01 18:17:27 字数 386 浏览 3 评论 0原文

当用作 html 属性中的值时,如何防止 HTML Tidy 转换 PHP 短标签?

这是它当前所做的一个示例。 它将这个: 转换

<input value='<?=$variable?>'>

为:

<input value='&lt;?=$variable?&gt;'>

我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

原野 2024-08-08 18:17:27

您可以预处理您的 php:

通过将 转换为

将变为

运行 HTMLtidy 后,您将执行相反的操作。

  1. 通过添加注释标签
  2. 进行预处理运行 HTMLtidy
  3. un-preprocess...

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.

  1. pre-process by adding comment tags
  2. run HTMLtidy
  3. un-pre-process...
三岁铭 2024-08-08 18:17:27

一种快速方法是将所有 替换为

A quick way is to just replace all <?= with <?php echo

淡墨 2024-08-08 18:17:27

即使它可以完成,Tidy 也不能很好地与 php 配合使用 - 如果你的 php 代码包含引号等,它可能会被阻塞。如果 php 输出缺少属性,Tidy 可能会抛出有关缺失属性的警告。

您可以在运行 Tidy 之前将所有 替换为 <#$variable#>,然后再将它们替换回来。
在控制台中,例如这样:

sed -i 's/<?=\(.*\)?>/<#\1#>/g' yourfile.php
tidy -m yourfile.php
sed -i 's/<#\(.*\)#>/<?=\1?>/g' yourfile.php

第一行将 转换为 <#$variable#> inside file,
第二个运行整齐并更新文件,
第三行将 <#$variable#> 转换为 返回

Even if it could be done Tidy doesn't work too well with php - it might choke if your php code contains quote marks etc. Also Tidy might throw warnings about missing attributes if they're output by php.

You could replace all <?$variable?> to <#$variable#> before running Tidy and then replace them back.
In the console, for example like this:

sed -i 's/<?=\(.*\)?>/<#\1#>/g' yourfile.php
tidy -m yourfile.php
sed -i 's/<#\(.*\)#>/<?=\1?>/g' yourfile.php

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>.

追风人 2024-08-08 18:17:27

未处理的 PHP 与 HTML 不兼容(除了少数最琐碎的情况),Tidy 仅对其提供表面支持。

实际上,Tidy 可能以无数种方式导致文档中的错误,因为它无法理解 PHP 生成的内容以及它如何与其他标记交互。

为了获得正确且可靠的结果,您应该对纯 HTML 输出进行后处理。 您可以通过在 PHP 中添加 Tidy 过滤器来做到这一点:

<?php
ob_start('ob_tidyhandler');
?>

这会影响运行时性能,但对于大多数站点来说这不是问题,因为 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:

<?php
ob_start('ob_tidyhandler');
?>

This will impact runtime performance, but it's not a problem for most sites as Tidy is quite fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文