简体   繁体   中英

How do I setAttribute with special characters of a DOMDocument node in PHP?

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?

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, eg

$pageHtml = $dom->saveHTML();
$pageHtml = preg_replace('|="%7B%7B%20(.*?)%20%7D%7D"|', '="{{ $1 }}"', $pageHtml);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM