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

前端 未结 5 1331
刺人心
刺人心 2020-11-28 05:49

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

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 06:30

    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, - _ . ! ~ * ' ( )

提交回复
热议问题