Pass & in the query string

I want to pass the '&' operator in the query string. I tried to use urlencode and urldecode but it does not work. I'm doing it:

$abc="A & B";
$abc2=urlencode($abc);

Then I pass a value like this

<a href="hello.php?say=<?php echo $abc2 ?>"><?php echo $abc;?></a>

and get the value on the next page as

$abc=$_GET['say'];
$abcd=urldecode($abc');
echo $abcd;

but the conclusion is not A & B

What am I doing wrong?

+3
source share
4 answers

This should basically work, I tried the following on my web server:

$a = 'A & B';
echo urlencode($a);
echo '<br />';
echo urldecode(urlencode($a));

Output

A+%26+B
A & B

I assume that you have another logical or syntactical error that forces you not to decode correctly, in your code there is an apostrophe in your urldecode () syntax.

Is this all your code or are you using "similar"? then your source code will be useful.

+2
source

Try the following:

$url = '?' . http_build_query(array(
  'say' => 'A & B'
));

// Then just:
echo $_GET['say'];

: http://codepad.viper-7.com/ibH79a?say=A+%26+B

+2

It seems you have added one quote next to $ abc on line 2 in the code below:

$abc = $_GET['say'];
$abcd = urldecode($abc);
echo $abcd;
0
source

& is a special HTML symbol, you need to convert it to &amp;.

Try the following:

$abc = "A & B";
<a href="hello.php?say=<?php echo htmlentities($abc); ?>"><?php echo $abc; ?></a>
-1
source

All Articles