How to properly URL encode a string in PHP?
For the URI query use urlencode
/urldecode
; for anything else use rawurlencode
/rawurldecode
.
The difference between urlencode
and rawurlencode
is that
urlencode
encodes according to application/x-www-form-urlencoded (space is encoded with+
) whilerawurlencode
encodes according to the plain Percent-Encoding (space is encoded with%20
).
How to encode URL in JS and Decode in PHP?
Try this in your javascript code
window.location.href = 'products.php?price_range='+encodeURIComponent('-INFto2000,2001to5000');
You can access the decoded value in $_GET['price_range']. $_GET variables are decoded by default in PHP.
Javascript - encodeURI() returing different results in different browsers
The problem is not encodeURI
(though as others have pointed out, you should never use encodeURI
, but rather encode individual components with encodeURIComponent
and then join them together).
The problem is that the dates for some reason contains lots of U+200E "LEFT-TO-RIGHT MARK"
characters (which are invisible, but still present), which once encoded become %E2%80%8E
.
Show us where/how you get the times, or filter the time strings to remove those characters before encoding.
php javascript url encoding
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.
How to encode only queryparams in a url string?
This would do
function encodeURI($url) {
// http://php.net/manual/en/function.rawurlencode.php
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
$unescaped = array(
'%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
'%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
);
$reserved = array(
'%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
'%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
);
$score = array(
'%23'=>'#'
);
return strtr(rawurlencode($url), array_merge($reserved,$unescaped,$score));
}
I found this here as an alternative to encodeURI()
of JS.
JavaScript encodeURIComponent vs PHP ulrencode
Use rawurlencode
instead of urlencode
in PHP.
What is the equivalent of JavaScript's encodeURIcomponent in PHP?
Try rawurlencode
. Or to be more precise:
function encodeURIComponent($str) {
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}
This function works exactly how encodeURIComponent
is defined:
encodeURIComponent
escapes all characters except the following: alphabetic, decimal digits,-
_
.
!
~
*
'
(
)
Related Topics
Escape String to Use in Mail()
Nginx Redirect Loop, Remove Index.PHP from Url
How to Send a Status Code in PHP, Without Maintaining an Array of Status Names
PHP Warning Permission Denied (13) on Session_Start()
Jquery Ajax Returns the Whole Page
PHP Pdo::Bindparam() Data Types.. How Does It Work
How to Sanitize Input with Pdo
PHP Function with Variable as Default Value for a Parameter
Why Is Constructing Pdo Connection Slow
Will a Script Continue to Run Even After Closing a Page
Paypal Smart Subscribe Server Side
How to Make a Multidimensional Array Unique
Autoloading Classes in PHPunit Using Composer and Autoload.Php