-3

I am trying to output the current page URL for Disqus comments. Since my page URL structure is like:

https://www.example.com/post/1234/2017-03-30/

I am having a little issue with escaping 2017-03-30.

I don't know if this has been asked before, I tried searching for reasonable answer but couldn't find any. I am trying to escape date, since including it like 2017-03-30 will only subtract it.

This is the PHP code that outputs Javascript:

echo 'this.page.url = "https://www.example.com/post/'. $id . '/\\'. $date . '\\/";';

Although the output would be:

this.page.url = "https://www.example.com/post/1234/\2017-03-30\/";

But the problem is Disqus will show the URL like:

https://www.example.com/post/1234/7-03-30

I know the problem is with \201 but what I don't know is how to fix it. I tried different ways. Nothing seems to be working. I am pretty much lost here. :/

JustCarty
  • 3,839
  • 5
  • 31
  • 51
  • 3
    I'm confused by the initial problem… what exactly is wrong with `2017-03-30`…? Where/what/how/when is subtracting it…? – deceze Mar 30 '17 at 16:12
  • 2
    I assume by "backslahing", you actually mean "escaping"? – gen_Eric Mar 30 '17 at 16:13
  • Andrew, do you mean like this? `echo 'this.page.url = "https://www.example.com/post/'. $id . '/\'. $date . '\/";';` – Dawood Khan Masood Mar 30 '17 at 16:13
  • 2
    Also, why do you have `'/\\'`? What's with all those slashes? What are you trying to do? Why not just `'/'`? Why do you need the other slashes? – gen_Eric Mar 30 '17 at 16:14
  • Removing only '\' would give: `Parse error: syntax error, unexpected '\'` – Dawood Khan Masood Mar 30 '17 at 16:15
  • @RocketHazmat, this is what Disqus comments will show the URL if it's only '/' and not '/\\': https://www.example.com/post/3509. And yes, I mean escaping. Sorry! – Dawood Khan Masood Mar 30 '17 at 16:19
  • 4
    I'm not sure I believe that `echo 'this.page.url = "https://www.example.com/post/'. $id . '/'. $date . '/";';` fails to work. Where is `this.page.url` appearing in your JavaScript? What do you see if you do `console.log(this.page.url);` right after? Where is Disqus showing you this "incorrect" URL? – gen_Eric Mar 30 '17 at 16:23
  • How can I avoid escaping \201 in "www.example.com/post/3576/\2017-03-30\/", that's my question. – Dawood Khan Masood Mar 30 '17 at 16:25
  • 1
    Does `echo 'this.page.url = "https://www.example.com/post/'. $id . '/'. $date . '/";';` *really* not work for you? I have no idea why you're trying to add backslahes to a URL. That's not the solution here. If this does not work, then the error is elsewhere in your (probably JavaScript) code. – gen_Eric Mar 30 '17 at 16:27
  • 1
    So, you've tried posting a comment from your page with that code in place, and it didn't appear with the correct URL in Disqus? – gen_Eric Mar 30 '17 at 16:34
  • Yup, that's the problem. – Dawood Khan Masood Mar 30 '17 at 16:36
  • 2
    Then the problem must be *elsewhere*. Probably in the JavaScript code you're using to post comments or something. I still fail to believe that part of the URL is missing when setting `this.page.url` properly (without any backslahes). – gen_Eric Mar 30 '17 at 16:37
  • 1
    Yeah, I guess. Tried replacing the URL with `document.URL;` and same problem; date missing. Guess I'll have to avoid using date entirely. Thank you, though! :) – Dawood Khan Masood Mar 30 '17 at 16:45

1 Answers1

3

In a JavaScript string, \201 is an octal escape, giving you the character U+0081, which is an unused control character and therefore not displayed.

If the backslash is actually needed, you will need to double-escape it so that the resulting JavaScript looks like \\2017-03-30\\, so the PHP would need to be \\\\'.$date.'\\\\

When you get this many backslashes, though, it's a sure sign you're doing something wrong.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592