0

I keep getting ILLEGAL STRING or ILLEGAL TOKEN. Any thoughts? I imagine this is an escaping issue, but have not found the correct solution.I am escaping the single quotes as other code is escaped in the same area.

I have tried:

  • escaping the special characters like & as well.

  • "Heredoc" and believe this is part of my solution, but still getting the errors.

Here is the code:

<?php
  $STRING .='
    <script type="application/javascript">

     function onPictureChanged() 
     {

       var href="http://pinterest.com/pin/create/button/?url="\' +   encodeURIComponent(location.href.replace(location.hash, "")) + \'"&media="\' + $(\'#fullResImage\').attr(\'src\');


       jQuery(\'.pp_social\').append(“<div class=\'pinterest\' ><a href=\'”+ href +”\' class=\'pin-it-button\' count-layout=\'horizontal\' target=\'_blank\'><img border=\'0\' src=\'http://assets.pinterest.com/images/PinExt.png\' title=\'Pin It\' /></a></div>”);

     }
    </script>';

echo $STRING;
6
  • You haven't closed your <?php tag (with ?>). Did you just forget to include it in the question? Commented Jul 22, 2014 at 17:29
  • I don't know what $STRING is meant for but there're several synthases that allow to just paste the code as-is. For instance, you could simply close the PHP block with ?>. Of course, no computer language I'm aware of accepts fancy quotes as string delimiter. Commented Jul 22, 2014 at 17:35
  • @ChrisMukherjee closing php tags ?> are not required at the end of a file Commented Jul 22, 2014 at 17:39
  • @oliakaoil I did not know that. Thanks for the info. However, it might still be a concern if that isn't the end of the file. Perhaps, he just pasted the relevant snippet of code, and in reality there is more code below. Commented Jul 22, 2014 at 17:43
  • @ChrisMukherjee from where does JQuery came from ?? Commented Jul 22, 2014 at 17:47

2 Answers 2

3

You use a ” instead of " in some cases. i.e. ”+ href +”\ should br "+ href +"\

PS. Mac User? You can disable that 'helper' under Preferences -> Keyboard -> Text

Sign up to request clarification or add additional context in comments.

6 Comments

Nice catch! This would definitely cause an ILLEGAL STRING or ILLEGAL TOKEN error.
@stefan from where does JQuery came from ??
@CodeLover jQuery is probably included in another part of the file or template. If this isn't included then an error like jQuery is not defined will be given but this is outside the scope of this question.
Yes, good catch on the quotes... but not a solve... Something I just noticed is that the error ILLEGAL TOKEN does not have an element after it. Such as if it were a misplaced ';' it would say 'ILLEGAL TOKEN ;' and same with ILLEGAL STRING, no element identified.
I also see a strange escape at "http://pinterest.com/pin/create/button/?url="\' + .... isnt that just "http://pinterest.com/pin/create/button/?url=" + .... ? (And so on in the string)
|
0

Ok, the answer was definitely the inaccurate escaping due to an attempt at many methods. The solution was a combination of the following:

  • Heredoc which greatly simplifies the escaping of output if not completely eliminates the need for it. Use variable within heredoc in PHP (SQL practice)
  • properly combining single and double quotes
  • Making sure no curly/fancy quotes were included... either from copy/pasting or my text editor ( See Alvaro's comment above)

@stefan Yes, you comment was definitely part of the issue! As the string was to be in a URL and not output to html.

Thanks for all you help. A fresh set of eyes always helps! Here is the final working code:

$STRING .= 

<<<MY_MARKER
<script type="application/javascript">

function onPictureChanged() 
{

    var href="http://pinterest.com/pin/create/button/?url="+encodeURIComponent(location.href.replace(location.hash,""))+"&media="+$('#fullResImage').attr('src');

    jQuery('.pp_social').append('<div class="pinterest"><a href="+ href +” class="pin-it-button" count-layout="horizontal" target="_blank"><img border="0" src="http://assets.pinterest.com/images/PinExt.png" title="Pin It" /></a></div>');

}

MY_MARKER;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.