I am loading a html file by file_get_contents and make some changes over it and then echo it. I want to add a javascript code to the file but I don't know how to do it. Thanks in advance.
<?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;
?>
<?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();
?>
This adds <script> alert(1); </script>
to . Of course you can use "getElementById" and it be will exactly the same.
Hope this helps.