PHP 中 JavaScript 的 encodeURI 组件的等价物是什么?

IT技术 php javascript encoding encodeuricomponent
2021-03-02 13:12:58

encodeURIcomponentPHP中 JavaScript 的功能相当于什么

5个回答

试试rawurlencode或者更准确地说:

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

这个函数的工作原理是如何encodeURIComponent定义的

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

@Gal:如果您需要相同的输出,则只需要该功能。
2021-04-19 13:12:58
谢谢!我以为我可以在没有函数的情况下做到这一点,但还好。
2021-04-23 13:12:58
我不相信 rawlurlencode 编码逗号(需要为 URI 组件完成)。
2021-04-25 13:12:58
谢谢秋葵。那很有帮助!
2021-05-08 13:12:58
@Gumbo 在 Google 图像搜索的 Firefox 扩展中,我注意到他们将图像传递给 javascript encodeURIComponent。请参阅addons.mozilla.org/nl/firefox/files/browse/126380/file/chrome/...,第 137 行。我如何在 PHP 中做同样的事情?
2021-05-15 13:12:58

你试了urlencode吗?

是的。问题是它不完全像 encodeURI,它转换每个字符,甚至 ^ 我想要一些功能完全相同的东西,而我不必干预 ^^。
2021-04-19 13:12:58

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

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

这是 switch 语句派上用场的地方
2021-04-19 13:12:58
答案的逻辑对我来说是有争议的,无论如何在这种情况下 IF 或 SWITCH 链都表现不佳。这是一个典型的案例,其中索引数组是最佳解决方案,其中查找值是键。
2021-04-19 13:12:58

这个代码怎么样?
我对每一层进行了编码。
实际上它与 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);
    }

}

/**
 * https://stackoverflow.com/questions/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));

}

http_build_query

这如何回答问题?
2021-05-10 13:12:58