PHP/HTML Tidy:锚定为name=no dons';似乎不起作用


PHP/HTML Tidy: anchor-as-name = no doesn't seem to work?

我使用PHP Tidy作为一个包含的脚本,虽然它似乎大部分(如果不完美的话)都能工作,但从标记中删除名称属性似乎并不起作用。我已经尝试了一切方法来删除它们,包括在运行Tidy之前使用PHP Simple HTML DOM删除它们,但它们只是不断地被放回。

我对这个问题进行了广泛的研究,但我得到的唯一结果是人们建议使用锚点作为名称,所以它一定有效,而我所做的只是有些不起作用。

我的Tidy配置如下,也许还有其他东西覆盖了锚作为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'
);

仔细想想,表演身体似乎也不起作用。。。也许整件事都被忽视了,而我做了其他根本错误的事情?

如有任何线索和协助,我们将不胜感激。

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>

所以整洁不仅添加了名称标签,尽管锚作为名称被设置为"否",它还产生了身体外的标签,尽管show body只被设置为是。

虽然显而易见的解决方案似乎是不使用整洁,但由于我只从简单的html dom中获得了上面几行的内容,我每天都在解析用Word可怜的html版本编写的数百万个字符以上的文件(500-1000页的文档),所以它对它的许多其他功能确实很有帮助。

来自文档:

[…]如果设置为"否",则如果存在id属性或已添加id属性,则会删除任何现有的name属性。

你还没有给出这方面的信息,所以我认为你只是没有为"它不起作用"的锚设置id。