Reputation: 685
I am trying to set up a template that will be a TWIG file using the DOM in PHP. So I set up the DOM and scrape the site I am getting the template from:
$dom = new DOMDocument('1.0');
$dom->loadHTMLFile('http://theurl.com');
then I modify the src of a script and save out the template:
foreach ($domNode->childNodes as $node) {
$node->setAttribute('src', "{{ asset('path/to/asset.js') }}");
}
$pageHtml = $dom->saveHTML();
$pageHtml is then saved out as a TWIG file:
file_put_contents('path/to/file.twig', $pageHtml);
When I look at this file, I now have as the script tag:
<script src="%7B%7B%20asset('path/to/asset.js')%20%7D%7D"></script>
What I need to have is:
<script src="{{ asset('path/to/asset.js') }}"></script>
So I somehow need to stop it doing url encoding. Any way to do this?
Upvotes: 2
Views: 658
Reputation: 74028
I don't know of any way to avoid this percent encoding.
One way to solve this, could be to replace the encoded characters before saving, e.g.
$pageHtml = $dom->saveHTML();
$pageHtml = preg_replace('|="%7B%7B%20(.*?)%20%7D%7D"|', '="{{ $1 }}"', $pageHtml);
Upvotes: 3
Reputation: 685
So the only answer I have at the moment is to modify $pageHtml before sending it out into the TWIG file with a preg_replace:
$pageHtml = preg_replace("|=\"%7B%7B%20asset|", "=\"{{ asset", $pageHtml);
$pageHtml = preg_replace("|\)%20%7D%7D\"|", ") }}\"", $pageHtml);
The first line will replace the start asset tag and the second line the end asset and will do css, javascript and any other areas that may use an asset tag.
I feel as though this is not the cleanest option, but at the moment it's the cleanest option I can find that works. If someone can come up with a better option I will set it as the answer.
Upvotes: 0