The difference between them is that the PHP's urlencode encodes spaces with + instead of %20?
What are the functions that do the same thing for both the languages?
Use rawurlencode instead of urlencode in PHP.
Follow this link at php's own documention rawurlencode
rawurlencode will do the trick, the link is for reference.
Actually even with JavaScript encodeURIComponent and PHP rawurlencode, they are not exactly the same too, for instance the '(' character, JavaScript encodeURIComponent will not convert it however PHP rawurlencode will convert it to %28. After some experiments and tips from others such as this question another Stackoverflow question.
I found the ultimate solution here.
All you need to do is use the add following code
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
They will be EXACTLY the same now, for example
fixedEncodeURIComponent(yourUrl) (JavaScript) = (PHP) rawurlencode(yourUrl)
no problem with decode, you can use decodeURIComponent() for JavaScript and rawurldecode for PHP
I was having the same problem between rawurlencode() and encodeURIComponent(). The difference for me was that I didn't discover the issue until using encodeURIComponent() in numerous source files, so going back to fix and change them all and then re-test everything was not an option.
Fortunately, JS gives you the ability to "hijack" built-in functions by assigning the same name to a new function. You can thus change the behavior of encodeURIComponent() with a very slight modification to Phantom1412's code, and without having to recode anything.
Just put this script in your page before your code makes any calls to encodeURIComponent():
var encodeURIComponentOld = encodeURIComponent;
encodeURIComponent = function(str) {
return encodeURIComponentOld(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
};
Related
I have a little issue with deleting a part of string using php.
Let me get you a little bit into problem, I have vlariable "column1text" in which is text string endered into < textarea >, but that does not matter. I want to remove all HTML tags from this string since they are doing problems sometimes. For example: When this string contains < /body > or < /table > it will do a lot of unwanted mess. So I want to get these tags away.
I am using $column1text = str_replace("TEXT TO REMOVE", "", $column1text); and it works, but I want to make function for it (optionaly if you know easier way, just tell me, I'll be glad).
I am using this function:
function remove($removetext)
{
$column1text = str_replace($removetext, "", $column1text);
}
And I am using it like this:
remove("TEXT TO REMOVE");
What am I doing wrong? (I am sure it's something pretty silly, but I cannot find it!)
P.S. I am totally sorry about my English, it must sound stupid, but I had no other idea than asking you.
You can either pass in $column1text as reference, or have your function returns the modified text (which I prefer)
function remove($column1text, $removetext) {
return str_replace($removetext, "", $column1text);
}
$column1text = remove($column1text, '<span>');
You are passing by value, when you actually need to be passing by reference. If you don't know what the difference is I suggest reading up a little on it.
Link
edit:
In a nutshell, whenever you pass a function a parameter, that function makes a copy of whatever it was passed, so when that parameter is manipulated in the function, it manipulates the copy and not the passed object itself.
When a function has a parameter passed by reference, then it actually has access to the object that was passed itself. That means anything you do to manipulate that object also affects the object outside of the function (since its not just a copy of it you are working with).
You need to tell interpreter that you want to use global variable, because now it sees it as local variable (within your function). To do this you just need to use global keyword:
function remove($removetext) {
global $column1text;
$column1text = str_replace($removetext, "", $column1text);
}
you can read more about it here http://php.net/manual/en/language.variables.scope.php
Friends, I am currently using the below function to encode my data and send them through GET method... I am using ajax to send and php to receive them...
function urlencode(a){
a=encodeURIComponent(a);
a=a.replace(/\\/g,'%5C');
a=a.replace(/!/g,'%21');
a=a.replace(/'/g,'%27');
a=a.replace(/\(/g,'%28');
a=a.replace(/\)/g,'%29');
a=a.replace(/\*/g,'%2A');
a=a.replace(/~/g,'%7E');
a=a.replace(/%20/g,'+');
a=a.replace(/%26amp%3B/gi,'%26');
a=a.replace(/%26lt%3B/gi,'%3C');
a=a.replace(/%26gt%3B/gi,'%3E');
a=a.replace(/%26nbsp%3B/gi,'%C2%A0');
return a;
}
after passing the string through encodeURIComponent(), I used extra replace() method so that the function encode the string for my link as similar as php...
Now my question is do anyone know any shorter or easy alternative way to do the same thing...
I place this question only for learning purpose... no jQuery please...
Using escape() instead of encodeURIComponent will eliminate at least some of the replace()'s you're using. E.g:
function urlencode(a){
return escape(a);
.replace(/\*/g,'%2A');
.replace(/%20/g,'+');
.replace(/%26amp%3B/gi,'%26');
.replace(/%26lt%3B/gi,'%3C');
.replace(/%26gt%3B/gi,'%3E');
.replace(/%26nbsp%3B/gi,'%C2%A0');
}
If you are sending GET requests from JS to PHP, you should use
encodeURIComponent(), for each parameter, on the javascript side
nothing on the PHP side (because $_GET is already urldecode()d; read the notes section in its linked documentation)
If you have to url-encode things in PHP, use urlencode(), which is guaranteed to be compatible with the corresponding urldecode().
If you want to learn how to do it "by yourself", read the source-code in the corresponding libraries to make sure you do not miss any scenario... it is generally a bad idea to re-implement library functions.
Hello everybody I'm trying to pass a parameter to my controller.php from javascript, but it doesn't pass and gives me error of undefined URL. kindly help me i shall be thankful to you...Here is my code
function JSfunction(assetid)
{
window.location="controller.php?command=delete&assetid=".assetid;
}
You're mixing PHP and JS, you use + to concatenate strings in JavaScript
Change the code to this and it should work:
function JSfunction(assetid) {
window.location="controller.php?command=delete&assetid=" + assetid;
}
What you are doing now is creating a string and accessing the assetid attribute of that string which is undefined.
you should set window.location to the full URL, not just the relative URL. I.E.
window.location="http://foo.com/controller.php?command=delete&assetid=" + assetid;
BTW, JS uses + to concat, not .
You must include server address, if it is being used locally,it can be denoted by http://localhost/AppName/pages?QueryString.
Concate string with plus sign, dot is used in php script for concatenation.
I was looking for a way to pass a string(variable saved in a form of $x) from php to Java Script and I found so many codes to solve that, but my question is : does those strings have to be declared global?!
i did declare it as a global variable but still no response ..!
any other suggestions?!
Pass a PHP string to a JavaScript variable (and escape newlines)
I tried most of these codes, none of them worked,
As the others have said, all we can say is that you are doing something wrong. You can place PHP variable values, strings or otherwise, wherever you want in your JavaScript code, since PHP is server-side and can do whatever you like on the client side.
This will help you to solved the issue of passing string variable value by calling the javascript function within php scrpt. :)
<?php
$testStrFileName = "test.jpg";
$file_name = "<script>". $testStrFileName ."</script>";
//<sample tags/>
echo 'Delete';
?>
I and lately I'm seeing h() and e() functions in PHP. I have googled them, but they are so short that results don't give any idea of what they are. I got results like exponential or math related functions. For example:
<td><?php echo h($room['Room']['message']) ?></td>
Does anyone have an idea? or maybe they are not called functions? (I think I read about that very long ago, but I can remember its real name)
ADDED:
Thanks, for the replies. I am using CakePHP and also found an e() example:
<?php e($time->niceShort($question['Question'] ['created'])) ?>
If they were escaping somehow strings I think it would make sense, since I always see them right next the "echo"
I still don't know what they are ;(
As several readers have said, these are CakePHP-specific short-cuts. You can find them in the API docs at: here (for CakePHP 2.x)
I think I read that some of these are going to be removed in 1.3, personally I never used e() as typing echo really doesn't take that much longer :)
edit: e() is deprecated in 1.3 and no longer available in 2.0 see here
It looks like it might be CakePHP.
See e()
e (mixed $data)
Convenience wrapper for echo().
This has been Deprecated and will be removed in 2.0 version. Use
echo() instead.
See h()
h (string $text, string $charset = null)
Convenience wrapper for htmlspecialchars().
Most likely, they are dummy functions someone introduced for the sake of brevity. The h(), for example, looks like an alias for htmlspecialchars():
function h($s)
{
return htmlspecialchars($s);
}
So look for them in the include files. Espec. the ones with names likes "util.php" or "lib.php".
Likely the framework you're using is doing some escaping and has defined some short hands for htmlentities and htmlspecialchars or equivalents.
I'd do a search on whatever framework you're using for "function h("
They are probably functions defined and implemented by the group's code that you're looking at. I'm not aware of any e/h functions in the PHP language.
Nothing here:
http://us3.php.net/manual/en/function.h.php
http://us3.php.net/manual/en/function.e.php
There aren't any functions in PHP called h() and e(). They must be declared in the project you are working on. search for them and find out what they do.
In CakePHP h() is:
Convenience wrapper for htmlspecialchars()
For more information about Global constants and functions in CakePHP view this link
http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html
I'd guess that h() escapes user-submitted data for safe output, and e() escapes for database insertion. Whatever the functionality, these are not stock PHP functions.
It's CakePHP.
echo h('some stuff')
Is just htmlspecialchar()ing the stuff.
If you are using a decent editor press ctrl and click on the function. It should take you to the function's declaration.
http://book.cakephp.org/view/121/Global-Functions these are shortcut functions in cakePHP
Many of them are deprecated in 1.3 so beware of using them yourself
h() is global function in CakePHP. Documents about h() for CakePHP version 2.5.7 : http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#global-functions
Laravel also use e() helper function to runs htmlentities over the given string.
echo e('<html>foo</html>');
// <html>foo</html>
documentation : https://laravel.com/docs/5.8/helpers#method-e