6

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

6 Answers 6

8

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>"; ?> 
Sign up to request clarification or add additional context in comments.

Comments

2

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

Alternatively, use a HEREDOC.

1 Comment

Thanks, I will look into the escape character.
2

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).

Comments

0

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)

Comments

0

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

echo <<<END lots of text here END; 

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.