问题描述
我需要编辑以下评论标签的内部,以便我可以更改css文件的位置。这需要通过PHP完成。
评论部分:
试试
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-> ;数据);
}
这将给出:
string(135)[if IE]>
< link rel =stylesheettype =text / csshref =http:// graphics8 (138)[if IE 6] .ytimes.com / css / 0.1 / screen / build / homepage / ie.css>
<![endif]
string(138) >
< link rel =stylesheettype =text / csshref =http://graphics8.nytimes.com/css/0.1/screen/build/homepage/ie6.css>
正如您所看到的,实际的评论节点是只是<! - 和 - > 部分。其余的只是文字。 [如果IE]> 不是实际标签的一部分。这是字符数据。
I need to edit the inside of the following comment tag so that I can change the location of the css file. This needs to be done through PHP. I have tried using the following code but I get invalid expression errors.
Comment Section:
<!--[if IE 6]><link rel="stylesheet" type="text/css" href="/Css/IE6.css" media="screen" /><![endif]-->Code that does not work:
$csslist = $xpath->query("//<!--[if IE 6]>"); foreach($csslist as $css) { $css->innertext = 'test'; }解决方案Try "//comment()" to get all CommentNodes.
Note that the link element in the CommentNode is not a childNode of the commentNode but mere data:
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 ); }This will give:
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]"As you can see, the actual comment node is just the <!-- and --> parts. The remainder is just text. The [if IE]> is not part of the actual tag. It is character data.
这篇关于用php查找和编辑html的注释元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!