我使用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版本 - 每天 - 所以它真的有助於它的許多其他功能。
你不應該在評論中發表額外的信息 - 請編輯你的問題,並附加一個例子。 – oezi 2012-03-21 12:51:39