Im using preg_match_all
to grab all scripts and place bfore body end like below:
preg_match_all('#(<script.*?</script>)#is', $html, $matches);
$js = '';
foreach ($matches[0] as $value):
$js .= $value;
endforeach;
$html = preg_replace('#(<script.*?</script>)#is', '', $html);
$html = preg_replace('#</body>#',$js.'</body>',$html);
This has broken some functionality on the page however for a few scripts like below:
<script data-template="bundle-summary" type="text/x-magento-template">
<li>
<strong class="label"><%- data._label_ %>:</strong>
<div data-container="options"></div>
</li>
</script>
How can i use the preg_match_all
to exclude <script data-template
scripts from being moved.
I figured i could check if the script x-magento-template
script by doing something like below:
if (strpos($value, 'type="text/x-magento-template"') === false) {
$js .= $value;
}
Then it won't be added to the $js
variable however am unsure how to stop the same scripts being deleted in the below line:
$html = preg_replace('#(<script.*?</script>)#is', '', $html);
I need to replace all scripts however not if they contain type="text/x-magento-template
Update
I did the below but am wondering if there is a more efficient way of doing this with preg_match_all?
preg_match_all('#(<script.*?</script>)#is', $html, $matches);
$js = '';
foreach ($matches[0] as $value):
if (strpos($value, 'type="text/x-magento-template"') === false) {
$js .= $value;
$html = str_replace($value, '', $html);
}
endforeach;
//$html = preg_replace('#(<script.*?</script>)#is', '', $html);
$html = preg_replace('#</body>#',$js.'</body>',$html);
After timing the difference between the method with the if statment and not the differences were negligible with a time of about 0.005 seconds each so am happy to leave it.