我集会尝试了很多。我用Google搜索了,但我仍然无法弄清楚如何解决这个问题。也就是说,我有相同的输出'这个文件无效'输入和警告。这是我的代码:
$correct_structure = ['<a>', '<div>', '</div>', '</a>', '<span>', '</span>'];
$incorrect_structure = ['<a>', '<div>', '</a>', '<p>' , '</tr>'];
function validate_html_tags($html_tags) {
$tags = htmlspecialchars(implode("", $html_tags));
$html_doc="<!DOCTYPE html>" . "<html><body>" . $tags . "</body></html>";
$dom = new DOMDocument;
$dom->loadHTML($html_doc);
$dom->saveHTML();
if ($dom->validate()) {
echo "This document is valid!\n";
} else {
echo "This document is invalid!\n";
}
return $tags;
}
The Warnings:
Warning: DOMDocument::validate(): No declaration for element html
Warning: DOMDocument::validate(): No declaration for element body in
This document is invalid!
答案 0 :(得分:0)
我能想到的一种方法是使用DOMDocument
。将数组连接成一个字符串,添加一些必要的东西,如Doctype,然后使用DOMDocument::validate
,如本手册页所示:
http://php.net/manual/en/domdocument.validate.php
例如:
$dom = new DOMDocument;
$dom->loadHTML($your_html_string);
if ($dom->validate()) {
echo "This document is valid!\n";
}