url encode和str_replace

I am trying to encode a sites current RFC 3986 standard and using this function:

function getUrl() {

      $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
      $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
      $url .= $_SERVER["REQUEST_URI"];

      $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
      $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");

      return str_replace($entities, $replacements, urlencode($url));

    }

The URL added : http://localhost/test/test-countdown/?city=hayden&eventdate=20160301 Returns: http://localhost/test/test-countdown/?city=hayden&eventdate=20160301 Not encoded with the // and & replaced

If you want encode an URL (not a site) in this format:

http%3A%2F%2Flocalhost%2Ftest%2Ftest-countdown%2F%3Fcity%3Dhayden%26eventdate%3D20160301

use the built-in php function rawurlencode( $url ).

While the canonical solution is to simply use rawurlencode() as fusion3k said, it's worth noting that, when rolling your own solution, you should:

  1. Listen more closely to the spec and encode all characters that are not either alphanumeric or one of -_.~.
  2. Be more lazy and refuse to type out all those entities. My rule of thumb is that I don't type of more than 10 array entries without a damn good reason. Automate!

Code:

function encode($str) {
    return preg_replace_callback(
        '/[^\w\-_.~]/',
        function($a){ return sprintf("%%%02x", ord($a[0])); },
        $str
    );
}

var_dump(encode('http://localhost/test/test-countdown/?city=hayden&eventdate=20160301'));

Result:

string(88) "http%3a%2f%2flocalhost%2ftest%2ftest-countdown%2f%3fcity%3dhayden%26eventdate%3d20160301"

Others have mentioned rawurlencode(), but the problem with your code is that you've got your arrays backwards.

Switch your arrays like this:

function getUrl() {

  $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
  $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
  $url .= $_SERVER["REQUEST_URI"];

  $entities = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");      
  $replacements = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');

  return str_replace($entities, $replacements, urlencode($url));
}

</div>