我正在使用 PHP Tidy 作为包含的脚本,虽然它似乎大部分(如果不完美)有效,但从我的标签中删除名称属性似乎不起作用。我已经尝试了一切来删除它们,包括在运行 Tidy 之前使用 PHP Simple HTML DOM 删除它们,但它们只是不断被放回原处。
我已经对这个问题进行了广泛的研究,但我得出的唯一结果是人们推荐使用锚作为名称,所以它必须有效,而且我正在做的事情只是有些东西不起作用。
我的 Tidy 配置如下,也许其他东西覆盖了 anchor-as-name 元素?我把它移到了底部,以防万一,但它似乎没有。我也尝试将其设置为 false,但这也无济于事。
$tidy_config = Array(
'break-before-br' => 'no',
'clean' => 'clean',
'doctype' => 'strict',
'drop-empty-paras' => 'yes',
'drop-font-tags' => 'yes',
'force-output' => 'yes',
'indent' => 'yes',
'indent-attributes' => 'no',
'indent-spaces' => 2,
'input-encoding' => 'utf8',
'join-styles' => 'no',
'literal-attributes' => 'yes',
'logical-emphasis' => 'yes',
'lower-literals' => 'yes',
'merge-divs' => 'no',
'merge-spans' => 'yes',
'output-encoding' => 'ascii',
'output-xhtml' => 'yes',
'output-bom' => 'no',
'preserve-entities' => 'yes',
'quiet' => 'yes',
'quote-ampersand' => 'yes',
'quote-marks' => 'no',
'quote-nbsp' => 'yes',
'show-body-only' => 'yes',
'show-errors' => 0,
'show-warnings' => 0,
'sort-attributes' => 'alpha',
'tidy-mark' => 'no',
'vertical-space' => 'yes',
'wrap' => '0',
'wrap-attributes' => 'no',
'anchor-as-name' => 'no'
);
想想看,show-body-only 似乎也不起作用……也许整个事情都被忽略了,而我在做其他根本错误的事情?
任何线索和帮助将不胜感激。
Oezi:感谢有关更新问题的提示。这是我在这里提出的第一个问题。
我正在使用 id 标签。这是通常发生的情况(所有相关变量均已定义):
require_once $docRoot . '/htmldom/simple_html_dom.php';
require $this_dir . '/includes/create-tidy-object.php';
$string1 = "<a id='anchor1'>First Anchor Text</a>";
$string2 = "<a id='anchor2' name='anchor2'>Second Anchor Text</a>";
$string3 = "<a id='anchor3'>Third Anchor Text</a>";
$tidy->parseString($string1,$tidy_config,'utf8');
$tidy->cleanRepair();
$revised_string_1 = $tidy;
print "<pre>Revised String 1:\n" . htmlentities($revised_string_1) . "\n\n";
$tidy->parseString($string2,$tidy_config,'utf8');
$tidy->cleanRepair();
$revised_string_2 = $tidy;
print "Revised String 2:\n" . htmlentities($revised_string_2) . "\n</pre>\n";
$stringdom3 = str_get_html($string3);
foreach($stringdom3->find('a[id]') as $anchor) { $anchor->name = null; }
$revised_string_3 = $stringdom3;
print "<pre>\nRevised String 3, after PHP Simple HTML DOM Parser:\n";
print htmlentities($revised_string_3) . "\n</pre>\n";
$tidy->parseString($revised_string_3,$tidy_config,'utf8');
$tidy->cleanRepair();
$revised_string_3a = $tidy;
print "<pre>Revised String 3, after going through both:\n";
print htmlentities($revised_string_3a) . "\n\n";
产生(为易读添加了换行符):
Revised String 1:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
</head>
<body>
<a id='anchor1' name="anchor1">First Anchor Text</a>
</body>
</html>
Revised String 2:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
</head>
<body>
<a id='anchor2' name='anchor2'>Second Anchor Text</a>
</body>
</html>
Revised String 3, after PHP Simple HTML DOM Parser:
<a id='anchor3'>Third Anchor Text</a>
Revised String 3, after going through both:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
</head>
<body>
<a id='anchor3' name="anchor3">Third Anchor Text</a>
</body>
</html>
所以 tidy 不仅添加了名称标签,尽管 anchor-as-name 设置为 no,它还在正文之外生成标签,尽管 show-body-only 设置为 yes。
虽然显而易见的解决方案似乎只是不使用 tidy,但由于我从简单的 html dom 中得到了我想要的上述行,我正在解析用 Word 编写的百万字符以上的文件(500-1000 页文档)的可悲HTML 版本——每天都有——所以它确实对它的许多其他功能很有帮助。