How can I add content to the newly created tag? For example, I need to create like the following tag:
<script src="https://stackoverflow.com/">
alert("ok");
</script>
I have implemented the following code:
$finalDom = new DOMDocument;
$finalDom->loadHTML("", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$newElement = $finalDom->createElement("script");
$newElement->setAttribute("src", "https://stackoverflow.com/");
$finalDom->appendChild($newElement);
The result of this code is an only empty script tag:
<script src="https://stackoverflow.com/"></script>
You can set the content using $textContent property of DOMElement.
$newElement = $finalDom->createElement("script");
$newElement->setAttribute("src", "https://stackoverflow.com/");
$newElement->textContent = 'alert("ok");';