What is the equivalent of JavaScript's encodeURIcomponent in PHP?
Asked Answered
R

5

107

What is the equivalent of JavaScript's encodeURIcomponent function in PHP?

Ringtail answered 14/11, 2009 at 13:37 Comment(1)
A nice comparison: devpro.it/examples/php_js_escaping.phpLorineloriner
G
165

Try rawurlencode. Or to be more precise:

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

This function works exactly how encodeURIComponent is defined:

encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )

Galloon answered 14/11, 2009 at 13:39 Comment(6)
Thank you! I thought I might be able to do it without a function, but ok.Ringtail
@Gal: You will only need that function if you need an identical output.Galloon
@Galloon In the firefox extension for the Google Image search I notice that they pass an image to the javascript encodeURIComponent. See addons.mozilla.org/nl/firefox/files/browse/126380/file/chrome/…, line 137. How could I do the same in PHP ?Manifesto
I don't believe rawlurlencode encodes commas (which needs to be done for URI components).Atwood
They are close, but rawurlencode and encodeURIComponent are not identical. For example, the former will encode single-quotes, while the latter will not.Rhythmandblues
In javascript encodeURIComponent(str) convert š to %C5%A1. While in php your function encodeURIComponent() convert š to %26scaron%3BTiemroth
P
8

Did you try urlencode ?

Pearson answered 14/11, 2009 at 13:39 Comment(1)
yes. the problem is that it's not entirely like encodeURI, it converts every character, even ^ I wanted something that would function the exact same way without me having to intervene ^^.Ringtail
S
3

function encodeURIComponent($string) {
   $result = "";
   for ($i = 0; $i < strlen($string); $i++) {
      $result .= encodeURIComponentbycharacter(urlencode($string[$i]));
   }
   return $result;
}

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

Selfcontained answered 2/6, 2010 at 19:4 Comment(2)
This is where a switch statement comes in handyDiopside
The logic of the answer is arguable to me, anyhow neither IF or SWITCH chains perform well in this case. This is a typical case in which an indexed array is the best solution, where the lookup value is the key.Derina
F
1

how is this code?
I encoded each tiers.
actually it's not same as encodeURI, but you can encode but host name and "/"

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://mcmap.net/q/205306/-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));

}
Fire answered 14/3, 2019 at 10:28 Comment(0)
J
-2

http_build_query

Jolda answered 14/11, 2009 at 13:56 Comment(1)
How does this answer the question?Moonscape

© 2022 - 2024 — McMap. All rights reserved.