2010-10-07 108 views
3

我有这个带有条件注释的html文件。PHP DomDocument更改条件注释

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <link rel="stylesheet" type="text/css" href="css/style.css"> 
     <link rel="stylesheet" type="text/css" href="css/elements.css"> 
     <title>Page</title> 
     <!--[if lte IE 6]> 
     <link rel="stylesheet" type="text/css" href="css/ie6.css" /> 
     <![endif]--> 
    </head> 
etc... 

我使用DOM文档库修改<link>属性。有什么方法让DomDocument读取和修改条件注释中的<link>元素。

回答

3
foreach($dom->getElementsByTagName('head') as $head) { 
    foreach($head->childNodes as $node) { 
     if($node instanceof DOMComment) { 
      $node->replaceData(16,60,'test'); 
     } 
    } 
} 

此代码的工作原理,我只是让你搜索如何获得replaceData方法的'偏移'和'计数'值!

+0

欢呼声,正是我所期待的 – 2010-10-07 08:44:08

+1

这是令人难以置信的,我无法找到任何互联网上的东西!我希望它可以帮助其他人^^ – MatTheCat 2010-10-07 08:47:09

+0

有没有办法让getElementsByTagName在评论中选择标签?循环遍历所有子节点以查找注释中的内容并不理想。 – chapmanio 2013-03-22 15:30:39