2012-03-21 38 views
0

我使用PHP Tidy作为包含脚本,虽然它似乎主要(如果不完美)的工作,它似乎并没有工作从我的标签中删除名称属性。我试过所有的东西去除它们,包括在运行Tidy之前使用PHP Simple HTML DOM去除它们,但它们只是不断放回去。PHP/HTML Tidy:anchor-as-name = no似乎不起作用?

我已经广泛研究过这个问题,但是我唯一的结果是是来自推荐使用锚点名称的人,所以它必须工作,并且只是关于我正在做的事情没有工作。

我的Tidy配置如下,也许别的什么东西重写锚点名称元素?我把它移到底部,以防万一,这似乎有帮助,但似乎并没有。我也尝试将它设置为假,而这也没有帮助。

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

那么整齐,不仅将名称标签,尽管锚-as-name被设置为否,它也在身体外部生成标签,尽管只将show-body-only设置为yes。

虽然显而易见的解决方案似乎只是不使用整洁,因为我只是从简单的HTML DOM得到我想要的上述行,我正在解析万字符以上的文件(500-1000页文档)写在Word的可怜的HTML版本 - 每天 - 所以它真的有助于它的许多其他功能。

回答

0

the documentation

[...]如果设置为 “无”,任何现有的name属性如果一个id属性存在或已被删除添加

你没有给这方面的消息,所以我想你刚才没有带一个ID设置为锚,其中“它不工作”。

+0

你不应该在评论中发表额外的信息 - 请编辑你的问题,并附加一个例子。 – oezi 2012-03-21 12:51:39