gpt4 book ai didi

php - 使用 php 查找和编辑 html 的评论元素

转载 作者:太空宇宙 更新时间:2023-11-04 15:40:59 25 4
gpt4 key购买 nike

我需要编辑下面注释标签的内部,以便我可以更改 css 文件的位置。这需要通过 PHP 来完成。我尝试使用以下代码,但出现无效表达式错误。

评论区:

<!--[if IE 6]><link rel="stylesheet" type="text/css" href="/Css/IE6.css" media="screen" /><![endif]-->

无效的代码:

    $csslist = $xpath->query("//<!--[if IE 6]>");    
foreach($csslist as $css) {
$css->innertext = 'test';
}

最佳答案

尝试 "//comment()"获取所有 CommentNode。

注意 CommentNode 中的 link 元素不是 commentNode 的子节点,而只是数据:

libxml_use_internal_errors(TRUE);
$dom = new DOMDocument;
$dom->loadHTMLFile( 'http://www.nytimes.com/' );
libxml_clear_errors();

$xpath = new DOMXPath($dom);
foreach( $xpath->query('//comment()[contains(., "link")]') as $node)
{
var_dump( $node->data );
}

这将给出:

string(135) "[if IE]>
<link rel="stylesheet" type="text/css" href="http://graphics8.nytimes.com/css/0.1/screen/build/homepage/ie.css">
<![endif]"

string(138) "[if IE 6]>
<link rel="stylesheet" type="text/css" href="http://graphics8.nytimes.com/css/0.1/screen/build/homepage/ie6.css">
<![endif]"

如您所见,实际的评论节点就是<!--。和 -->部分。其余的只是文本。 [if IE]>不是实际标签的一部分。是字符数据。

关于php - 使用 php 查找和编辑 html 的评论元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3328631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com