pieroxy / lz-string

LZ-based compression algorithm for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Quotes in the compressed string

YuccaV2 opened this issue · comments

Hello,
I have a problem with a UTF16 string compression. I need to inject it into a javascript variable from php. But sometimes in the encoded string there are single or double quotes, which breaks my variable (ex: myVar="...㤗妵ឯ "ᢡ怣..."). How to declare a js variable generated in php with, for content, a UTF16 compressed string in which there can be single or double quotes? Thanks in advance... I'm tearing my hair out.

May be : json_encode ?

Hello,

In order to transform a String to its literal representation in JavaScript, you just need to do a few replacements into your original string. You need to escape double quotes but also carriage returns, probably looking like:

  1. Transform all \ to \\
  2. Transform all " to \"
  3. Transform all ' to \'
  4. Transform all char 10 to \r
  5. Transform all char 13 to \n

I am pretty sure there are some library to do this out there.
I would start looking here: https://stackoverflow.com/questions/7085648/how-to-escape-string-from-php-for-javascript

Note that this problem has nothing to do with LZ-String, so that I am closing the github issue.

Thanks, json_encode seems to work well in my case