PHP DOMDocument createElement模糊了&符号

Par*_*lia 5 php obfuscation domdocument

我正在使用PHP DOMDocument库来创建一个'script'标签元素,其中包含一个javascript值,并将其插入DOM中.

这就是我做的:

$scriptElement = $doc->createElement('script',$scriptTagVal);
echo $scriptElement->nodeValue;                                                                  
$someNode->parentNode->insertBefore($scriptElement,$postRttScriptNode); 
Run Code Online (Sandbox Code Playgroud)

这表现我预期的方式,它在'someNode'之前插入一个元素.然而,它做了一些奇怪的事情,它混淆了&符号(&).单个&符号(&)不存在,双&符号(&&)被买下单个&符号.

疯?好吧,我试过这样做:

$scriptElement = $doc->createElement('script','return "undefined" !== typeof b **&&** null !== b ? b.getAttribute("content") : 0');
Run Code Online (Sandbox Code Playgroud)

如果我回显$ scriptElement-> nodeValue,

我明白了

return "undefined" !== typeof b **&** null !== b ? b.getAttribute("content") : 0'
Run Code Online (Sandbox Code Playgroud)

我假设这几乎闻所未闻,但我尝试使用包含双&符号的值创建不同的元素.就像是:

 $scriptElement = $doc->createElement('p','Why does DOMDocument obfuscate double
            ampersands(&&)');    
Run Code Online (Sandbox Code Playgroud)

输出到我的浏览器的结果是

带有值的标签:

为什么DOMDocument会混淆双&符号(

也许特殊人物?无论如何我可以解决这个问题吗?我的意思是,肯定有一种方法可以使用DOMDocument在HTML中插入javascript,对吧?

Scu*_*zzy 7

http://www.php.net/manual/en/domdocument.createelement.php中所述

该值不会被转义.使用DOMDocument :: createTextNode()创建具有转义支持的文本节点

有了它你可以尝试:

$doc = new DomDocument();
$scriptContent = 'return "undefined" !== typeof b **&&** null !== b ? b.getAttribute("content") : 0';
$scriptElement = $doc->createElement('script');
$scriptElement->appendChild($doc->createTextNode($scriptContent));
$doc->appendChild($scriptElement);
echo $doc->saveXML();
Run Code Online (Sandbox Code Playgroud)

您可能需要$doc->ownerDocument->createTextNode()在构建文本节点时使用,这是未经测试的,因为我没有可运行的代码段.