What Is the Equivalent of JavaScript's Encodeuricomponent 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, - _ . ! ~ * ' ( )

javascript encodeURIComponent equivalent in php

Actually rawurldecode() is giving you the correct result. That character consists of four bytes when encoded in utf-8 and the rule in url encoding is to convert each byte to %XX notation. rawurldecode() is giving you back those 4 bytes but probably you have not set your page's encoding to utf-8 so your browser is misinterpreting those bytes. add this to your <head>:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

and you should see the right character.


This is a test page I made:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php echo rawurldecode('%F3%BE%AE%A2'); ?>
</body>
</html>

what I see in my browser:

br>

exactly the character you want to see.

JavaScript encodeURIComponent vs PHP ulrencode

Use rawurlencode instead of urlencode in PHP.

urlencode() from PHP in JavaScript?

There is no function quite matching urlencode(), but there is one quite equivalent to rawurlencode(): encodeURIComponent().

Usage: var encoded = encodeURIComponent(str);

You can find a reference here:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent

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 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.



Related Topics



Leave a reply



Submit