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:
- Transform all
\
to\\
- Transform all
"
to\"
- Transform all
'
to\'
- Transform all char 10 to
\r
- 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