什么等于PHP中JavaScript的encodeURIcomponent?


Answers:


141

尝试rawurlencode。或者更确切地说:

function encodeURIComponent($str) {
    $revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
    return strtr(rawurlencode($str), $revert);
}

该功能的工作原理完全是如何encodeURIComponent被定义的

encodeURIComponent 转义除以下字符外的所有字符:字母,十进制数字, - _ . ! ~ * ' ( )


谢谢!我以为没有功能也可以做到,但是还可以。
加仑(Gal)

1
@Gal:仅在需要相同输出时才需要该功能。
Gumbo

@Gumbo在Google Image搜索的firefox扩展中,我注意到它们将图像传递给javascript encodeURIComponent。参见addons.mozilla.org/nl/firefox/files/browse/126380/file/chrome/…,第137行。如何在PHP中执行相同的操作?
user410932

谢谢浓汤。那很有帮助!
Marvin Thobejane 2013年

我不相信rawlurlencode会编码逗号(URI组件需要这样做)。
克里斯·雷

6

你尝试了urlencode吗?


1
是。问题在于,它并不完全像encodeURI,它会转换每个字符,即使^我想要的东西也能以完全相同的方式运行而无需我干预^^。
加尔(Gal)

2

函数encodeURIComponent($ string){
   $ result =“”;
   for($ i = 0; $ i <strlen($ string); $ i ++){
      $ result。= encodeURIComponentbycharacter(urlencode($ string [$ i]));
   }
   返回$ result;
}

函数encodeURIComponentbycharacter($ char){ 如果($ char ==“ +”){返回“%20”; } 如果($ char ==“%21”){返回“!”; } if($ char ==“%27”){return'“';} 如果($ char ==“%28”){返回“(”;} 如果($ char ==“%29”){return“)”; } 如果($ char ==“%2A”){返回“ *”; } 如果($ char ==“%7E”){返回“〜”; } 如果($ char ==“%80”){返回“%E2%82%AC”; } 如果($ char ==“%81”){返回“%C2%81”; } 如果($ char ==“%82”){返回“%E2%80%9A”; } 如果($ char ==“%83”){返回“%C6%92”; } 如果($ char ==“%84”){返回“%E2%80%9E”; } 如果($ char ==“%85”){返回“%E2%80%A6”; } 如果($ char ==“%86”){返回“%E2%80%A0”; } 如果($ char ==“%87”){返回“%E2%80%A1”; } 如果($ char ==“%88”){返回“%CB%86”; } 如果($ char ==“%89”){返回“%E2%80%B0”; } 如果($ char ==“%8A”){返回“%C5%A0”; } 如果($ char ==“%8B”){返回“%E2%80%B9”; } 如果($ char ==“%8C”){返回“%C5%92”; } 如果($ char ==“%8D”){返回“%C2%8D”; } 如果($ char ==“%8E”){返回“%C5%BD”; } 如果($ char ==“%8F”){返回“%C2%8F”; } 如果($ char ==“%90”){返回“%C2%90”; } 如果($ char ==“%91”){返回“%E2%80%98”; } 如果($ char ==“%92”){返回“%E2%80%99”; } 如果($ char ==“%93”){返回“%E2%80%9C”; } 如果($ char ==“%94”){返回“%E2%80%9D”; } 如果($ char ==“%95”){返回“%E2%80%A2”; } 如果($ char ==“%96”){返回“%E2%80%93”; } 如果($ char ==“%97”){返回“%E2%80%94”; } 如果($ char ==“%98”){返回“%CB%9C”; } 如果($ char ==“%99”){返回“%E2%84%A2”; } 如果($ char ==“%9A”){返回“%C5%A1”; } 如果($ char ==“%9B”){返回“%E2%80%BA”; } 如果($ char ==“%9C”){返回“%C5%93”; } 如果($ char ==“%9D”){返回“%C2%9D”; } 如果($ char ==“%9E”){返回“%C5%BE”; } 如果($ char ==“%9F”){返回“%C5%B8”; } 如果($ char ==“%A0”){返回“%C2%A0”; } 如果($ char ==“%A1”){返回“%C2%A1”; } 如果($ char ==“%A2”){返回“%C2%A2”; } 如果($ char ==“%A3”){返回“%C2%A3”; } 如果($ char ==“%A4”){返回“%C2%A4”; } 如果($ char ==“%A5”){返回“%C2%A5”; } 如果($ char ==“%A6”){返回“%C2%A6”; } 如果($ char ==“%A7”){返回“%C2%A7”; } 如果($ char ==“%A8”){返回“%C2%A8”; } 如果($ char ==“%A9”){返回“%C2%A9”; } 如果($ char ==“%AA”){返回“%C2%AA”; } 如果($ char ==“%AB”){返回“%C2%AB”; } 如果($ char ==“%AC”){返回“%C2%AC”; } 如果($ char ==“%AD”){返回“%C2%AD”; } 如果($ char ==“%AE”){返回“%C2%AE”; } 如果($ char ==“%AF”){返回“%C2%AF”; } 如果($ char ==“%B0”){返回“%C2%B0”; } 如果($ char ==“%B1”){返回“%C2%B1”; } 如果($ char ==“%B2”){返回“%C2%B2”; } 如果($ char ==“%B3”){返回“%C2%B3”; } 如果($ char ==“%B4”){返回“%C2%B4”; } 如果($ char ==“%B5”){返回“%C2%B5”; } 如果($ char ==“%B6”){返回“%C2%B6”; } 如果($ char ==“%B7”){返回“%C2%B7”; } 如果($ char ==“%B8”){返回“%C2%B8”; } 如果($ char ==“%B9”){返回“%C2%B9”; } 如果($ char ==“%BA”){返回“%C2%BA”; } 如果($ char ==“%BB”){返回“%C2%BB”; } 如果($ char ==“%BC”){返回“%C2%BC”; } 如果($ char ==“%BD”){返回“%C2%BD”; } 如果($ char ==“%BE”){返回“%C2%BE”; } 如果($ char ==“%BF”){返回“%C2%BF”; } 如果($ char ==“%C0”){返回“%C3%80”; } 如果($ char ==“%C1”){返回“%C3%81”; } 如果($ char ==“%C2”){返回“%C3%82”; } 如果($ char ==“%C3”){返回“%C3%83”; } 如果($ char ==“%C4”){返回“%C3%84”; } 如果($ char ==“%C5”){返回“%C3%85”; } 如果($ char ==“%C6”){返回“%C3%86”; } 如果($ char ==“%C7”){返回“%C3%87”; } 如果($ char ==“%C8”){返回“%C3%88”; } 如果($ char ==“%C9”){返回“%C3%89”; } 如果($ char ==“%CA”){返回“%C3%8A”; } 如果($ char ==“%CB”){返回“%C3%8B”; } 如果($ char ==“%CC”){返回“%C3%8C”; } 如果($ char ==“%CD”){返回“%C3%8D”; } 如果($ char ==“%CE”){返回“%C3%8E”; } 如果($ char ==“%CF”){返回“%C3%8F”; } 如果($ char ==“%D0”){返回“%C3%90”; } 如果($ char ==“%D1”){返回“%C3%91”; } 如果($ char ==“%D2”){返回“%C3%92”; } 如果($ char ==“%D3”){返回“%C3%93”; } 如果($ char ==“%D4”){返回“%C3%94”; } 如果($ char ==“%D5”){返回“%C3%95”; } 如果($ char ==“%D6”){返回“%C3%96”; } 如果($ char ==“%D7”){返回“%C3%97”; } 如果($ char ==“%D8”){返回“%C3%98”; } 如果($ char ==“%D9”){返回“%C3%99”; } 如果($ char ==“%DA”){返回“%C3%9A”; } 如果($ char ==“%DB”){返回“%C3%9B”; } 如果($ char ==“%DC”){返回“%C3%9C”; } 如果($ char ==“%DD”){返回“%C3%9D”; } 如果($ char ==“%DE”){返回“%C3%9E”; } 如果($ char ==“%DF”){返回“%C3%9F”; } 如果($ char ==“%E0”){返回“%C3%A0”; } 如果($ char ==“%E1”){返回“%C3%A1”; } 如果($ char ==“%E2”){返回“%C3%A2”; } 如果($ char ==“%E3”){返回“%C3%A3”; } 如果($ char ==“%E4”){返回“%C3%A4”; } 如果($ char ==“%E5”){返回“%C3%A5”; } 如果($ char ==“%E6”){返回“%C3%A6”; } 如果($ char ==“%E7”){返回“%C3%A7”; } 如果($ char ==“%E8”){返回“%C3%A8”; } 如果($ char ==“%E9”){返回“%C3%A9”; } 如果($ char ==“%EA”){返回“%C3%AA”; } 如果($ char ==“%EB”){返回“%C3%AB”; } 如果($ char ==“%EC”){返回“%C3%AC”; } 如果($ char ==“%ED”){返回“%C3%AD”; } 如果($ char ==“%EE”){返回“%C3%AE”; } 如果($ char ==“%EF”){返回“%C3%AF”; } 如果($ char ==“%F0”){返回“%C3%B0”; } 如果($ char ==“%F1”){返回“%C3%B1”; } 如果($ char ==“%F2”){返回“%C3%B2”; } 如果($ char ==“%F3”){返回“%C3%B3”; } 如果($ char ==“%F4”){返回“%C3%B4”; } 如果($ char ==“%F5”){返回“%C3%B5”; } 如果($ char ==“%F6”){返回“%C3%B6”; } 如果($ char ==“%F7”){返回“%C3%B7”; } 如果($ char ==“%F8”){返回“%C3%B8”; } 如果($ char ==“%F9”){返回“%C3%B9”; } 如果($ char ==“%FA”){返回“%C3%BA”; } 如果($ char ==“%FB”){返回“%C3%BB”; } 如果($ char ==“%FC”){返回“%C3%BC”; } 如果($ char ==“%FD”){返回“%C3%BD”; } 如果($ char ==“%FE”){返回“%C3%BE”; } 如果($ char ==“%FF”){返回“%C3%BF”; } 返回$ char; }


这是切换语句派上用场的地方
Yada 2015年

2
对于我来说,答案的逻辑是有争议的,无论如何,在这种情况下,无论是IF链还是SWITCH链都不能表现良好。这是典型的情况,其中索引数组是最佳解决方案,其中查找值是关键。
Emanuele Del Grande

0

这段代码怎么样?
我对每个层进行了编码。
实际上,它与encodeURI不同,但是您可以对主机名和“ /”进行编码

function encodeURI($url) {
    if(__empty($url))return $url; 

    $res = preg_match('/.*:\/\/(.*?)\//',$url,$matches);
    if($res){

        // except host name
        $url_tmp = str_replace($matches[0],"",$url);

        // except query parameter
        $url_tmp_arr = explode("?",$url_tmp);

        // encode each tier
        $url_tear = explode("/", $url_tmp_arr[0]);
        foreach ($url_tear as $key => $tear){
            $url_tear[$key] = rawurlencode($tear);
        }

        $ret_url = $matches[0].implode('/',$url_tear);

        // encode query parameter
        if(count($url_tmp_arr) >= 2){
            $ret_url .= "?".$this->encodeURISub($url_tmp_arr[1]);
        }
        return $ret_url;
    }else{
        return $this->encodeURISub($url);
    }

}

/**
 * /programming/4929584/encodeuri-in-php/6059053
 */
function encodeURISub($url) {
    // http://php.net/manual/en/function.rawurlencode.php
    // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
    $unescaped = array(
    '%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
    '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
            );
    $reserved = array(
            '%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
            '%40'=>'@','%26'=>'&','%3D'=>'=','%24'=>'$'
    );
    $score = array(
            '%23'=>'#'
    );
    return strtr(rawurlencode($url), array_merge($reserved,$unescaped,$score));

}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.