Php 文件问题

Php 文件问题,php,domdocument,Php,Domdocument,我有以下代码: $DOMDocument = @DOMImplementation::createDocument(null, 'html'); $width = array(200, 700); $colgroup = $DOMDocument->createElement('colgroup'); foreach($width as $key => $width) { $precentageWidth = round(($width*100)/array_sum($w

我有以下代码:

$DOMDocument = @DOMImplementation::createDocument(null, 'html'); 
$width = array(200, 700);

$colgroup = $DOMDocument->createElement('colgroup');
foreach($width as $key => $width) {

   $precentageWidth = round(($width*100)/array_sum($width));

   $col = $DOMDocument->createElement('col');
   $col->setAttribute('width', 'test');

   $colgroup->appendChild($col);

}

$baseelement = $DOMDocument->documentElement;

$baseelement->appendChild($colgroup);


echo $DOMDocument->saveHTML();
预期结果:

<html><colgroup><col width="test"></col><col width="test"></col></colgroup></html>

得到结果:

<html><colgroup><col width="test"><col width="test"></colgroup></html>

所以我的问题是:
标记丢失了;为什么?

结束标记是禁止的

这类似于-你永远不会写
,是吗


因为结束标记是禁止的,所以DOM实现会忽略它。

感谢您的快速回复。你有/知道这方面的解决方法吗?因为标签是禁止的,所以没有解决方法。但是我想您可以创建一个XML文档。它不应该生成
吗?如果这是XHTML,那当然。但事实并非如此。它是HTML<代码>/>在HTML中并不是真正的“东西”。证明:
的行为与
不同。即使它是并且确实是这样,
实际上也是
,这是被禁止的。@NiettheDarkAbsol这不是100%正确的。
/>
并不意味着结束标记,它只是说我们有一个不能包含内容的空标记<因此,代码>无效<代码>无效元素是指其内容模型在任何情况下都不允许其包含内容的元素。注意;这可以通过使用str_替换来修复。。注意:您不会在Chrome浏览器中看到标签!