Passing & in query string

前端 未结 4 1276
误落风尘
误落风尘 2021-01-21 08:59

I want to pass \'&\' operator in query string. I tried to use urlencode and urldecode but its not working. I am doing this:

$abc=\"A & B\";
$abc2=urlenc         


        
相关标签:
4条回答
  • 2021-01-21 09:06

    Basically it should work I tried the following on my webserver:

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

    Output

    A+%26+B
    A & B
    

    I guess you have another logically or syntax error which causes your & not to be decoded correctly, in your code there's an apostroph in your urldecode()-syntax.

    Is that all of your code or are you using a "similiar" one? your original code would be useful then.

    0 讨论(0)
  • 2021-01-21 09:06

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

    $abc = $_GET['say'];
    $abcd = urldecode($abc);
    echo $abcd;
    
    0 讨论(0)
  • 2021-01-21 09:15

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

    Try this:

    $abc = "A & B";
    <a href="hello.php?say=<?php echo htmlentities($abc); ?>"><?php echo $abc; ?></a>
    
    0 讨论(0)
  • 2021-01-21 09:17

    Try this:

    $url = '?' . http_build_query(array(
      'say' => 'A & B'
    ));
    
    // Then just:
    echo $_GET['say'];
    

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

    0 讨论(0)
提交回复
热议问题