Reputation: 4187
I am using DomDocument to modify a HTML file that is resaved in a new folder.
There are a few PHP variables I am adding into the HTML elements, but I need to be able to add some variables into a section of javascript.
How can I achieve this?
Alternatively, is there any other way to take a HTML file and insert PHP variables into it and save the result? Maybe a super basic and lightweight template engine? (Must be able to read the HTML into memory)
Example HTML:
<html>
<body>
<script>
var my_array = new Array();
</script>
<body>
</html>
Example PHP:
$doc->DOMDocument();
$doc->loadHTML($html_from_file);
// somehow need to target my_array;
$doc->saveHTMLFile($filename);
Upvotes: 0
Views: 296
Reputation: 416
I do not know if I understand very well your question but in principle you could access the script tag in the DOM as any other tag with, for example:
$script = $doc->getElementsByTagName('script')->item(0) // if you want the first match
Now you can retrieve and modify, if needed, its content with the nodeValue property.
Upvotes: 1
Reputation: 3330
You can just create a script tag with PHP, like this:
echo "<script>var blah = $blah;</script>";
Upvotes: 0