I'm having problems passing utf-8 strings to javascript (ajax). Currently i'm using rawurlencode on the PHP side and unescape on the javascript side.
The problem is in latin and rawurlencode doesn't support it fully.
Is there any alternative or any better option?
The solution was in json_encode functions. The problems stopped when i added JSON_HEX_APOS|JSON_HEX_QUOT.
Thanks!
use json_encode in PHP and receive responses as JSON (jQuery is helpful)
ajax is sent in utf-8 by default, so You just have to return utf-8
php's utf8_encode(data) gets an ISO-8859-1 string as the data argument.
need more suggestions? Tell me where You get the text from ;)
From experience, Javascript's escape() (ant thus unescape()) are not Unicode (UTF-8) friendly. Use encodeURIComponent() and decodeURIComponent() instead.
Anyway, as the docs says:
The escape() function should not be
used to encode URIs.
If php is doing the encoding and js decoding whay not simply not encode in php and encode in js as well? Not really an answer so much as a work around i guess.
Related
I have a simple `url that pass two parameters. Name and cellphone. But when I use special characters, the parameter can't be decoded. It appears the ?? instead of the character.
I already tried use urldecode($_GET['name']), rawurldecode, html_entity_decode, utf8_decode, but none of this worked.
I have the utf-8 meta tag in my HTML and I also tryed pass this as a header inside php, but it didn't work.
The code is like this
<?php echo $_GET['name']; ?>
You simply have the use the correct function, which is utf8_encode:
<?php echo utf8_encode($_GET['name']); ?>
Output:
Consultório
The function utf8_encode:
This function converts the string data from the ISO-8859-1 encoding to
UTF-8.
See the documentation here.
name=Consult%F3rio
This is the good old ISO-8859-1 encoding for Consultório of the early days of the web. If the decoded version renders incorrectly, it's very likely that your application is not using ISO-8859-1 at all, thus there's no benefit in using it there either. If your app is using UTF-8, the simplest solution would be to switch entirely to UTF-8:
Consult%C3%B3rio
This is basically what you get with any builtin PHP function when fed with UTF-8 data because they work at byte level:
var_dump(rawurlencode('Consultório')); // string(16) "Consult%C3%B3rio"
If this happens to be third-party data you can't control, please check Martin's answer.
I am getting data from a web API which has a strange encoding. I am using PHP and can't seem to decode input strings. I seem to be having this problem, which explains what's going but doesn't really help me figure out how to fix it.
Can anyone help?
Thanks!
You may want to try analyzing the encoding using something like mb_detect_encoding().
http://www.php.net/mb_detect_encoding
You can use mb_detect_encoding() to detect the encoding of the strings.
If they are not what you are expecting, you can use mb_convert_encoding() to convert to something like UTF-8 or whatever you want.
I use encodeURI and decodeURI to send French accented text from AS3 to my mysql database and get it back in AS3 and it works fine.
Except that I also need to work on the text on the server where it's hopelessly mangled. php functions urldecode and rawurldecode only do part of the job. some coding remains.
Can you use: encodeURIComponent() on the js side and then use urldecode() on the php side?
On the server side the PHP code will be using urldecode() function to decode, but the JavaScript code is responsible to encode the URL. Which of the following JavaScript function is compatible with the PHP urldecode() function:
escape()
encodeURI()
encodeURIComponent()
You can use either encodeURI or encodeURIComponent. The php manual states:
Decodes any %## encoding in the given string.
So whatever the encoding function encodes, all %## sequences are decoded. So you can use either one of the JavaScript functions to encode it.
edit:
(In kind-of response to Gumbo's answer, which he removed?)
php's urldecode also decodes + signs to spaces (because it implements a different standard). To make sure that no plus signs that are actually intended are decoded on the php side, just use encodeURIComponent to be sure. That encodes + to %2B, which is then again safe from php's urldecode.
I need to escape entire javascript code block using escape() compatible function via PHP, and then put resulting JavaScript code back into a code construct like this:
document.write(unescape(ESCAPED_JS));
I'm not trying to increase security by doing this, protect code, or anything like that. Just to make it a bit harder to glance over a code and see what it does.
Does anyone have a working solution for this, or idea how to do it? The only reference I found about it is on this page, but it only deals with unescaping JS-escaped string using PHP, but by taking special care of UTF-8 characters (which I also need to consider).
escape is not a standard function. Better use encodeURIComponent or JSON instead.
Gumbo is right (as always), but I think rawurlencode and rawurldecode are the php equivalents of js escape and unescape
You should be able to use urlencode and urldecode to do this.
http://php.net/manual/en/function.urlencode.php