Decoding in php a text coded with encodeURI - php

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?

Related

Convert characters to decoded html then back to encoded on submitted to page

I have special characters like # & and so on. What was suggested is I html decode these characters before passing them to my PHP image generator (a font previewer like things remembered).
I have no issues turning the text into html decoded, but how do I turn them back to encoded on the page the text gets submitted to?
The PHP superglobals $_GET and $_POST should be automatically decoded if you send a properly encoded data from the client side, I.E. using encodeURIcomponent() js function.
If it doesn't seem to be decoded for some reason, you should find out why (maybe you double-encoded it?), or bypass it like with urldecode() php func (there is also a workaround for utf8 decoding).

php urldecode not working correctly when receiving response from ajax in javascript

I have explained all the steps below and captured data at each stage.
I can't understand why urldecode is not working correctly in this case?
Original String:
<p>​hello</p>
Javascript encodeURIComponent() of above string:
%3Cp%3E%E2%80%8Bhello%3C%2Fp%3E
Sent via Ajax:
%3Cp%3E%E2%80%8Bhello%3C%2Fp%3E
PHP: Echo $_REQUEST['string'];
%3Cp%3E%E2%80%8Bhello%3C%2Fp%3E
PHP: urldecode of above string
​hello
There's a special hidden character between > and h. You'll be able to see it if you view the string as html in a browser.

Pass special characters in PHP

I have a text field where where the user can pass wild cards - more specific to the question they can use '%' character.
I am using ajax to get the value and send it to a PHP file. If I enter '%BA' in the text file and retrieve the value using
document.getElementById('textfield').value
This actually gets '%BA'. I am using POST method to send it to a PHP file. But the variable displays as "�" in the web browser and inserts " ° - degree small o" in the database.
I am sure there are other cases that I am not aware of as well. Is there a function in PHP to escape the special characters or any other way to get the exact string?
Edit: This may be a guess but doing escape(document.getElementById('textfield').value) to send the value and using urldecode($values[3]) to retrieve the value doesn't work. Maybe it's a js to PHP problem.
Update: urldecode will not work. Read the first comment in urldecode. Used the function there. Solved.
while passing the value using ajax , you just encode the value with encodeURIComponent() function and use urldecode() function to decode it in the php file. This might solve the issue.
You could encode the characters with urlencode (and maybe htmlspecialchars too) before storing it in the database, and use urldecode ( and maybe htmlspecialchars_decode) to decode them before displaying to the user.
You can use escape in javascript i.e.
escape(document.getElementById('val'))

Passing utf-8 strings between php and javascript

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.

Passing Hebrew encoded text by AJAX (windows 1255)

When making an AJAX call from the client, the server responds with Hebrew text. The PHP page that returns the result is in Windows-1255.
For some reason, the result encoding is not Windows-1255, and all I see is gibberish instead of Hebrew.
The result purposely contains HTML tags.
I've tried urlencoding, base64 encoding, nothing works.
Thanks!
Can't you change it so that the server return UTF-8 instead of 1255?
There's absolutely no reason these days to continue using ANSI.
all i needed to do is put this in the respone php file
header('Content-Type: text/html; charset=windows-1255');
10x everyone!!!
beware: AJAX works only with UTF8, so be sure to convert characters before sending them, otherwise you can get mangled characters in Javascript.
you should also not use the string from get data,
create a new srting...
What encoding is the web page containing the AJAX call/display?

Categories