将javascript添加到由php中的file_get_内容加载的html文件中

将javascript添加到由php中的file_get_内容加载的html文件中,javascript,php,Javascript,Php,我正在一个文件一个文件地加载html文件,获取内容并对其进行一些更改,然后对其进行回显。我想在文件中添加一个javascript代码,但我不知道怎么做。提前谢谢 <?php $page = file_get_contents('http://www.example.com/index.html'); $doc = new DOMDocument(); $doc->loadHTML($page); $node = $doc->getElementById('something')

我正在一个文件一个文件地加载html文件,获取内容并对其进行一些更改,然后对其进行回显。我想在文件中添加一个javascript代码,但我不知道怎么做。提前谢谢

<?php
$page = file_get_contents('http://www.example.com/index.html');
$doc = new DOMDocument();
$doc->loadHTML($page);
$node = $doc->getElementById('something');
$node->parentNode->removeChild($node);
echo $doc->saveHtml($doc), PHP_EOL;
?>
这增加了警报1;到当然,您可以使用getElementById,它将完全相同


希望这有帮助。

显然您已经知道如何加载DOM和删除节点。这与添加节点没有太大区别。脚本标记只是一个包含文本代码的HTML节点。我总觉得你在试图建立一个仿冒网站,这将模拟真实的网站并向你发送用户密码……我已经检查了$dom->createElement和$dom->appendChild,但我无法使它工作。任何帮助都会很好。
<?php

$page = file_get_contents('http://example.com');
$doc = new DOMDocument();
$doc->loadHtml($page);

$head = $doc->getElementsByTagName("head")->item(0);

$js = $doc->createDocumentFragment();
$js->appendXml("<script> alert(1); </script>");
$head->appendChild($js);

echo $doc->saveHtml();

?>