I have a string with unicode characters that I am transferring via HTTP. This string was encoded with Javascript's encodeURIcomponent(). Is there an equivalent function in php to Javascript's decodeURIComponent()?
urldecode()
However you do not need to use it on $_REQUEST variables, which are already decoded automatically.
rawurldecode()
which does not decode plus to space charactor
Related
My PHP application outputs JSON where special characters are encoded, f.ex. the string "Brøndum" is represented as "Br\u00f8ndum".
Can you tell me which encoding this is, as well as how I get back from "Br\u00f8ndum" to "Brøndum".
I have tried utf8_encode/decode but they don't work as expected.
Thanks!
That's standard JSON unicode escaping.
You get back to the actual character by using a JSON parser. json_decode in the case of PHP.
You can tell PHP not to escape Unicode characters in the first place with the JSON_UNESCAPED_UNICODE flag.
json_encode("Brøndum", JSON_UNESCAPED_UNICODE)
mb_detect_encoding is your function. You just pass it the string and it detects the codification. You can also send it an array with the possibilities (as a regular string like "hello" could potentially be encoded in different codifications.
echo mb_detect_encoding("Br\u00f8ndum");
I'm using parse_str to get a raw value from a URL (which is obviously entered by the user), and I'm wondering if there's anything I should to to make it safe before I use it (i.e. convert special characters like '<').
I noticed that the function does remove some characters, but I couldn't find the specifics anywhere.
Thanks.
You can use htmlentities() and then parse_str() or parse_url() function
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'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.
I have a html text. I had encoded it in php using urlencode function.
I want to decode that text in the javascript.
when i use unescape function in javascript it replaces all the special characters back but sapce is replaced by '+'. how can i do it correctly so that space is replaced as space itself???
PHP rawUrlEncode() == JavaScript encodeURIComponent()
PHP rawUrlDecode() == JavaScript decodeURIComponent()
Try using rawurlencode instead - urlencode does some things differently for "historical" reasons.
See http://us.php.net/manual/en/function.urlencode.php for more information.
Parenthesis are exceptions to all of what is said in this post.
geek mode on :
false
PHP rawUrlEncode() !== JavaScript encodeURIComponent()
but true
PHP rawUrlEncode() == JavaScript encodeURIComponent()
In other words, there are many special characters that aren't treated as safe in rawurlencode when they are in encodeURIComponent.
Try this:
return decodeURIComponent((str + '').replace(/\+/g, '%20'));
Source: http://phpjs.org/functions/urldecode:572
PHP rawUrlEncode with JS unescape (deprecated but working)
For me decodeURIComponent throw errors sometimes.