How to echo script in php

后端 未结 6 1111
再見小時候
再見小時候 2021-01-20 04:11

I am using the shortcode execute plugin in wordpress.

This simply allows me to write shortcode like this [email_spamproof]

But I am trying to echo a script.

相关标签:
6条回答
  • 2021-01-20 04:19

    It is easy. Save your javascript code in a new one file and then use this to load it:

    include('myjavascript.php');
    

    Then you will use the include option as a echo because the web will understand your code as HTML and you execute it when you want with php (like echo).

    0 讨论(0)
  • 2021-01-20 04:25

    You could write " in your javasript code, and then only use single quotes(') in your php echo.

    A more suitable method is to use escape characters in your javascript code "\'" (without the double quotes)

    0 讨论(0)
  • 2021-01-20 04:31

    The \ character is the escape character. Prepend one to each internal use of the character used to delimit the string.

    Alternatively, use a HEREDOC.

    0 讨论(0)
  • 2021-01-20 04:32

    You can either escape them with a back slash: echo 'It\'s cold'; or use heredoc:

    echo <<<END
    lots of text here
    END;
    
    0 讨论(0)
  • 2021-01-20 04:39

    The right code:

    <?php
    
    echo "<script type='text/javascript'>
        <!-- 
        // spam protected email
        emailE=('enquiries@' + 'example.co.uk')
        document.write('<a title='E-mail Example' href='mailto:' + emailE + ''>' + emailE + '</a>')
         //-->
        </script>
        <noscript>
            <span class='spam-protected'>Email address protected by JavaScript. Please enable JavaScript.</span>
        </noscript>";
    
    ?>
    
    0 讨论(0)
  • 2021-01-20 04:39

    You need to escape them and then concatenate the variables :

    (Also I dont know if you realize, but you are echoing a commented out section of javascript)

    echo '<script type="text/javascript">    
      <!--      // spam protected email     
      emailE=("enquiries@" + "example.co.uk")     
      document.write(\'<a title="E-mail Example" href="mailto:'.emailE.'">'.emailE.'</a>\')      
      //-->     
      </script>     
      <noscript>         
      <span class="spam-protected">Email address protected by JavaScript. Please enable JavaScript.</span>     
      </noscript>'; 
    
    0 讨论(0)
提交回复
热议问题