使用Tidy清理HTML,HTML内容被改变,编码问题?

发布于 2024-08-13 11:18:05 字数 692 浏览 3 评论 0原文

我正在从 smarty 模板中获取 HTML,需要清理它(只是想删除多余的空格,并很好地格式化/缩进 HTML),我正在使用 tidy 来做类似的事情:


$html = $smarty->fetch('foo.tmpl');

$tidy = new tidy;
$tidy->parseString($html, array(
    'hide-comments' => TRUE,
    'output-xhtml' => TRUE,
    'indent' => TRUE,
    'wrap' => 0
));
$tidy->cleanRepair();
return $tidy;

虽然这对于英语来说没问题,但似乎多语言支持打破这个。例如,我在 $html 中可以使用阿拉伯字符,但整理后我会得到一些令人讨厌的编码:

هل أنت متأÙсØ´ أنÙς تريش

有一个设置吗in tidy 会格式化 HTML,但不理会 HTML 本身?我看了这篇文章:PHP“漂亮的打印”HTML(不是Tidy)但这似乎行不通,因为我是从 smarty 获取 HTML 的。

任何建议表示赞赏。

I am fetching HTML from a smarty template and need to clean it (simply want to remove extra whitespace, and format / indent the HTML nicely), I'm using tidy to do something like:


$html = $smarty->fetch('foo.tmpl');

$tidy = new tidy;
$tidy->parseString($html, array(
    'hide-comments' => TRUE,
    'output-xhtml' => TRUE,
    'indent' => TRUE,
    'wrap' => 0
));
$tidy->cleanRepair();
return $tidy;

While this works ok for english, multilingual support seems to break this. For example, I have arabic characters ok in $html, but after tidy I get back some nasty encoding:

هل أنت متأكد أنك تريد

Is there a setting in tidy that will format the HTML, but leave the HTML itself alone? I looked at this post: PHP "pretty print" HTML (not Tidy) but it's seems like this won't work since I'm grabbing my HTML from smarty.

Any suggestions appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

感受沵的脚步 2024-08-20 11:18:05

尝试使用第二个参数设置 parseString

http://www.php 中的编码.net/manual/en/tidy.parsestring.php

Try using the second argument to set the encoding in parseString

http://www.php.net/manual/en/tidy.parsestring.php

土豪我们做朋友吧 2024-08-20 11:18:05
$html = $smarty->fetch('foo.tmpl');

$tidy = new tidy;
$tidy->parseString($html, array(
    'hide-comments' => TRUE,
    'output-xhtml' => TRUE,
    'indent' => TRUE,
    'wrap' => 0
            ),
'raw');
$tidy->cleanRepair();
return $tidy;

使用raw作为编码参数
对于 raw,Tidy 将输出高于 127 的值,而不将其转换为实体,并且所有阿拉伯字符都高于 127

$html = $smarty->fetch('foo.tmpl');

$tidy = new tidy;
$tidy->parseString($html, array(
    'hide-comments' => TRUE,
    'output-xhtml' => TRUE,
    'indent' => TRUE,
    'wrap' => 0
            ),
'raw');
$tidy->cleanRepair();
return $tidy;

use raw as encoding parameter
For raw, Tidy will output values above 127 without translating them into entities and all Arabic characters are above 127

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