PHP DomDocument改变条件注释

时间:2010-10-07 06:06:00

标签: php html dom document

我有这个带有条件评论的html文件。

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

我正在使用DomDocument库来修改<link>属性。有没有办法让DomDocument读取和修改条件注释中的<link>元素。

1 个答案:

答案 0 :(得分:3)

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

此代码有效,我只是让您搜索如何获取replaceData方法的'offset'和'count'值!