Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo script in php

Tags:

php

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. Please see below...

<?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>';

?>


Now you can probably see my problem already.

Usually when I echo stuff it goes like this... echo 'hello';

And you break the string using the same apostrophes - like this...
echo 'hello ' . php_name() . ' and friends'

My Problem

The script also uses a similar method by adding script variables into the string, but these script apostrophes will get confused as PHP apostrophes, and break the echoed string.

How can I avoid this?

Thanks

like image 892
Joshc Avatar asked Aug 07 '12 11:08

Joshc


1 Answers

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>";

?>
like image 132
Michal Grohman Avatar answered Sep 20 '22 20:09

Michal Grohman