php javascript url encoding - php

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.

Related

Convert french accent to specific encoding in PHP

In php, what function can I use to convert the text 'pétition' to 'p%E9tition'.
I have tried with uft8_encode and uft8_decode with no success.
%E9 is an URL encoded escape character. You can achieve this by urlecode($string).
If you want HTML escaping, you can either use htmlentities($string) (more encoding) or htmlspecialchars($string) (less encoding).
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.htmlentities.php
http://php.net/manual/en/function.htmlspecialchars.php
When dealing with UTF-8 strings, you will need to decode the string (ie. with utf8_decode) before encoding with urlencode to be used in a query part of a URL.
print_r( urlencode(utf8_decode('pétition')) );
// p%E9tition
You can try to have a look at htmlentities.
This link can help

Which JavaScript function is compatible with PHP urldecode 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.

CKEditor / Ajax special characters encoding / decoding not working

I'm using CKEditor, and typing in some text with special characters: "Bâtisseurs passionnés", note french special characters. I then use javascript escape() to get the input and send it via AJAX/JSON to the PHP server script.
On the PHP side of things, the log output looks like the following before and after using urldecode(), it appears to convert the tag parts but the special characters only show up as '?' and stored as such into the database. Is there another call I should be using? Or are special characters not included for urldecode?
$json = json_decode($data);
error_log("URLDecode: before: " . $data);
error_log("URLDecode: after: " . urldecode($data));
and the output looks like
URLDecode: before: %3Cp%3E%0A%09B%E2tisseurs%20passionn%E9s%3C/p%3E%0A
URLDecode: after: <p>
B?tisseurs passionn?s</p>
escape isn't a match pair for php's urldecode.
Use encodeURIComponent in javascript.
escape and unescape Functions
The escape and unescape functions do not work properly for non-ASCII characters and have been deprecated. In JavaScript 1.5 and later, use encodeURI, decodeURI, encodeURIComponent, and decodeURIComponent.
MDC:functions

What is the equivalent of JavaScript's decodeURIcomponent in PHP?

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

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.

Categories