dqwh2717
2016-02-18 17:49
浏览 36
已采纳

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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试编码网站当前的RFC 3986标准并使用此功能:

  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'); 
 $ replacementments = array('!','*',“  '“,”(“,”)“,”;“,”:“,”@“,”&“,”=“,”+“,”$“,”,“,”/“,”?  “,”%“,”#“,”[“,”]“); 
 
返回str_replace($ entities,$ replacementments,urlencode($ url)); 
 
} 
    
 
 

添加的网址: http:// localhost / test / test-countdown /?city = hayden& eventdate = 20160301 Returns: http: // localhost / test / test-countdown /?city = hayden& eventdate = 20160301 未编码 // & 已替换

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanjinchen5296 2016-02-18 17:56
    已采纳

    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 ).

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drpzr64329 2016-02-18 18:16

    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"
    
    评论
    解决 无用
    打赏 举报
  • dongqin6926 2016-02-18 18:44

    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>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题