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

Viewed 72175

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

5 Answers

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://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));

}
Related