2

I am currently trying to add this code to a PHP file:

if (contains($current_url, $bad_urls_2)) {
    echo '<script>
           $('body :not(script,sup)').contents().filter(function() {
               return this.nodeType === 3;}).replaceWith(function() {
               return this.nodeValue.replace(/[®]/g, '<sup>$&</sup>');
           });</script>';
}

the issue with this is that it outputs this error:
Error: syntax error, unexpected 'body' (T_STRING), expecting ',' or ';': syntax error, unexpected 'body' (T_STRING), expecting ',' or ';'

The if statement is not necessary for this issue, as it only checks if there is a word in an URLs and then applies this code only to the pages that match them.

2
  • 1
    You have ' (single quote) before body which closes the sting you started on the previous line Commented Oct 6, 2019 at 7:38
  • @VLAZ thanks for saying this, this acutally also broke the code. Commented Oct 6, 2019 at 8:12

1 Answer 1

1

Use heredoc https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

<?php

if (contains($current_url, $bad_urls_2)) {
    echo <<<JS
<script>
$('body :not(script,sup)').contents().filter(function() {
   return this.nodeType === 3;}).replaceWith(function() {
   return this.nodeValue.replace(/[®]/g, '<sup>$&</sup>\);
});
</script>
JS;
}
Sign up to request clarification or add additional context in comments.

Comments