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;
davidsbro
  • 2,761
  • 4
  • 23
  • 33
  • You haven't closed your ``). Did you just forget to include it in the question? – Chris Mukherjee Jul 22 '14 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. – Álvaro González Jul 22 '14 at 17:35
  • @ChrisMukherjee closing php tags `?>` are not required at the end of a file – oliakaoil Jul 22 '14 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. – Chris Mukherjee Jul 22 '14 at 17:43
  • @ChrisMukherjee from where does JQuery came from ?? – Avinash Babu Jul 22 '14 at 17:47
  • @CodeLover I don't know, I guess it was included somewhere else in the file. Are you trying to help me prove that this is indeed a snippet of the full code? I'm not sure I quite understand the purpose of your question. – Chris Mukherjee Jul 22 '14 at 21:24

2 Answers2

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

Stefan
  • 1,248
  • 6
  • 23
  • Nice catch! This would definitely cause an `ILLEGAL STRING` or `ILLEGAL TOKEN` error. – Chris Mukherjee Jul 22 '14 at 17:45
  • @stefan from where does JQuery came from ?? – Avinash Babu Jul 22 '14 at 17:47
  • @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. – Stefan Jul 22 '14 at 17:51
  • 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. – user3865639 Jul 22 '14 at 18:11
  • 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) – Stefan Jul 22 '14 at 18:17
  • This was originally copy/pasted from here:http://websolstore.com/how-to-add-pinterest-to-social-media-icons-in-prettyphoto/ but I retyped it after many other errors. BTW, the string error is UNEXPECTED STRING. – user3865639 Jul 22 '14 at 18:17
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;
Community
  • 1
  • 1