This question already has answers here:
Is it possible to preserve plus signs in PHP $_GET vars without encoding?
(7 answers)
Closed 9 years ago.
I am retrieving a GET variable from URL which contains "+" in between the string.
But PHP is storing "space" instead of "+"
PFB output :
in URL : key=7VR47WOtmD+acS0
php echo : echo rawurlencode$_GET['key']) ;
//returns
7VR47WOtmD acS0
Could you please tell me how to get + symbol in the php output ?
Note: do not suggest find and replace option
You need to encode reserved characters ($ & + / : ; ? # etc) if you want to use them in an URL. You need to use %2B instead of +. If you're using PHP to generate the URL, then you can use the function urlencode() to automatically encode all characters that require encoding.
Yes, that's how URLs work. The character + in URLs must, because of the URL specification, be interpreted as a space, as plain spaces are not allowed in URLs. If you want to send a true + character (or a space for that matter) you must escape it using something like encodeURIComponent("+") (in the browser) or urlencode("+") in PHP, before sending it.
Related
This question already has answers here:
Difference between * and + regex
(7 answers)
Closed 2 years ago.
My PHP code receives a $request from an AJAX call. I am able to extract the $name from this parameter. As this name is in German, the allowed characters also include ä, ö and ü.
I want to validate $name = "Bär" via preg_match. I am sure, that the ä is correctly arriving as an UTF-8 encoded string in my PHP code. But if I do this
preg_match('/^[a-zA-ZäöüÄÖÜ]*$/', $name);
I get false, although it should be true. I only receive true in case I do
preg_match(utf8_encode('/^[a-zA-ZäöüÄÖÜ]*$/'), $name);
Can someone explain this to me and also how I set PHP to globaly encode every string to UTF-8?
PHP strings do not have any specific character encoding. String literals contain the bytes that the interpreter finds between the quotes in the source file.
You have to make sure that the text editor or IDE that you are using is saving files in UTF-8. You'll typically find the character encoding in the settings menu.
Your regular expression is wrong. You only test for one sign. The + stands for 1 or more characters. If your PHP code is saved as UTF-8 (without BOM), the u flag is required for Unicode.
$name = "Bär";
$result = preg_match('/^[a-zA-ZäöüÄÖÜ]+$/u', $name);
var_dump($result); //int(1)
For all German umlauts the ß is still missing in the list.
I am trying to send a url like this for search data
http://localhost/project/search/text:75%
I am getting 400 - Bad Request error in here.
I even tried replacing percentage with %25. But it didn't worked. How should I send the search data containing percentage?
In URLs, the % percent character is reserved for character encoding.
Usually to represent a % character you can use %25, but as you have already tried this and that it doesn't work for you, you should instead use PHP's urlencode function like so:
$url=urlencode("text:%75");
The same issue occurs with :, this therefore prevents the same issue with this character also (which for reference is %3A).
Partially from this question.
This question already has answers here:
PHP URL Encoding / Decoding
(4 answers)
Closed 8 years ago.
I can print a url with the following:
<?php print $base_url . $node_url ?>
What is the standard PHP way of converting special characters?
So instead of: http://time.com/3525666/ebola-psychology-fear-symptoms/
I need http%3A%2F%2Ftime.com%2F3525666%2Febola-psychology-fear-symptoms%2F
You would use urlencode for that sort of escaping.
Other escaping functions exist for other purposes, like htmlspecialchars for making text output safely for HTML display.
use his function in php , it is built in function to encode in url format
urlencode();
Just to add, htmlspecialchars, as mentioned above in the comment can take care of few html entities, not all of them.
Use htmlentities() instead:
$query_string = 'foo=' .urlencode($foo) . '&bar=' . urlencode($bar);echo '';
This question already has answers here:
htmlentities() vs. htmlspecialchars()
(12 answers)
Closed 8 years ago.
I have read their documentation, but I still don't get when to use each of them and their difference.
Let's consider the situation of having a general string in a variable and needing to echo it inside HTML code. If it has any HTML markup in it, I want it converted to HTML code (< replaced by <, & replaced by &. If it has UTF special chars that aren't available in HTML code, it's replaced by HTML number (• replaced by •).
What's the best function for that?
A harder need: unprintable chars, like \n, char(10), char(13), etc, be replaced by their number code, in the case the string is printed inside <pre> or any special textarea so that the string be dumped.
htmlentities is a workaround for not having set the character type of the document properly. htmlspecialchars is the correct function to use for merely writing text into an HTML document.
As to your second question, I think you're looking for addcslashes.
My problem is this. I'm setting a cookie using java script, which contains value
"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK+Hntg"
Now, when i receive and read this cookie on a PHP page, im getting the content as
"MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK Hntg"
The '+' sign is change to a space character. Why is it so?
According to document.cookie reference:
The cookie value string can use encodeURIComponent() to ensure that
the string does not contain any commas, semicolons, or whitespace
(which are disallowed in cookie values).
So this is what you need to do:
document.cookie = "foobar=" + encodeURIComponent("MXGWJfgr4HDINl/BdAfBUf12710aFNcaIQKgGJ7VShxvprVo1XK+Hntg");
alert(document.cookie); // + becomes %2B which PHP will interpret and decode automatically