How to properly URL encode a string in PHP?

I'm making a search page, where you type a search query and the form is submitted to search.php?query=your query . What PHP function is the best and that I should use for encoding/decoding the search query?


For the URI query use urlencode / urldecode ; for anything else use rawurlencode / rawurldecode .

The difference between urlencode and rawurlencode is that

  • urlencode encodes according to application/x-www-form-urlencoded (space is encoded with + ) while
  • rawurlencode encodes according to the plain Percent-Encoding (space is encoded with %20 ).

  • The cunningly-named urlencode() and urldecode().

    However, you shouldn't need to use urldecode() on variables that appear in $_POST and $_GET .


    Here is my use case, which requires an exceptional amount of encoding. Maybe you think it contrived, but we run this on production. Coincidently, this covers every type of encoding, so I'm posting as a tutorial.

    Use case description

    Somebody just bought a prepaid gift card ("token") on our website. Tokens have corresponding URLs to redeem them. This customer wants to email it to someone else. Our web page includes a mailto link that lets them do that.

    PHP code

    // The order system generates some opaque token
    $token = 'w%a&!e#"^2(^@azW';
    
    // Here is a URL to redeem that token
    $redeemUrl = 'https://httpbin.org/get?token=' . urlencode($token);
    
    // Actual contents we want for the email
    $subject = 'I just bought this for you';
    $body = 'I just bought a widget for you. Please enter your shipping details here: ' . $redeemUrl;
    
    // A URI for the email as prescribed
    $mailToUri = 'mailto:?subject=' . rawurlencode($subject) . '&body=' . rawurlencode($body);
    
    // Print an HTML element that links to that page
    echo '<a href="' . htmlspecialchars($mailToUri) . '">Email your friend</a>';
    

    Note: the above assumes you are outputting to a text/html document. If your output media type is text/json then simply use $retval['url'] = $mailToUri; because output encoding is handled by json_encode() .

    Test case

  • Run the code on a PHP test site (is there a canonical one I should mention here?)
  • Click the link
  • Send the email
  • Get the email
  • Click that link
  • You should see:

    "args": {
      "token": "w%a&!e#"^2(^@azW"
    }, 
    

    And of course this is the JSON representation of $token above.

    链接地址: http://www.djcxy.com/p/92766.html

    上一篇: 是否可以在MAILTO链接的主体中添加HTML链接

    下一篇: 如何在PHP中正确地URL编码一个字符串?