2

I have a link like this:

http://domain.com/page.php?product=brown+rice

And then in the page.php page I GET the product:

 $product = $_GET['product'];



 <?php echo $product; ?>

But it only prints : brown rice without the +

How to get the + in the get method?

alisa
  • 259
  • 3
  • 15

8 Answers8

3

First, I will replace it with any different symbol before I pass it in the link:

For example:

     http://domain.com/page.php?product='.str_replace("+","_",$obj->items).'"

And then in the page.php I will turn it back to the original symbol:

  $product= str_replace("_","+",$_GET['product']);

This method will only replace all products which have + symbol in the url. And when you GET it, it will check if the name of the product will have the symbol.

I hope that you understand and it helps

Al Kasih
  • 887
  • 5
  • 24
2

First URL encode and then decode using

urlencode() and urldecode()

<?php
$_GET['url'] = '123abc+456';
echo $_GET['url'];
echo "<br/>";
echo $_GET['url'] = urlencode($_GET['url']);

echo "<br/>";
echo str_replace('%2B', '+', $_GET['url']);
?>

Variables for $_GET considers + as a space.

URL encode adds %2B against space.

Then decode it with string replace.

Demo: http://codepad.org/67QVYHY3

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • can you make it with my code above. Because it's a little complicated for me to interpret it – alisa Dec 08 '14 at 07:37
  • Please don't make the example for the urlencode or str_replace in the `echo` but in the `GET` itself instead. – alisa Dec 08 '14 at 07:41
2

Might be help

echo isset($_GET['product']) ? urlencode($_GET['product']) : 'Product Not Found';
Mahesh.D
  • 1,691
  • 2
  • 23
  • 49
1

Please refer the below url, it is already answerd in stackoverflow,

PHP - Plus sign with GET query

If you'll read the entirety of that bug report you'll see a reference to RFC 2396. Which states that + is reserved. PHP is correct in translating an unencoded + sign to a space.

You could use urlencode() the ciphertext when returning it to the user. Thus, when the user submits the ciphertext for decryption, you can urldecode() it. PHP will do this automatically for you if it comes in via the GET string as well.

Bottom line: The + must be submitted to PHP as the encoded value: %2B

Community
  • 1
  • 1
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
1
//page 1
$criteria = urlencode("brown+rice");
$URL = "http://example.com/page.php?product={$criteria}";
//page 2
$product = $_GET['product'];
echo $product;
Kh An
  • 481
  • 6
  • 13
0

Replace + with it's representation %2B

Justinas
  • 41,402
  • 5
  • 66
  • 96
0

You could read out $_SERVER["QUERY_STRING"]

preg_match_all('/(\w+)=([^&]+)/', $_SERVER["QUERY_STRING"], $set);
$_GET = array_combine($set[1], $set[2]);
A.B
  • 20,110
  • 3
  • 37
  • 71
0

Just write http://example.com/page.php?product=brown%2Brice

%2B changes to plus, because B2 (hex) is a '+' in ASCII table

AKK
  • 44
  • 4