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

JavaScript 的 encodeURIcomponent 函数在 PHP 中的等价物是什么?

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

推荐答案

Try rawurlencode.或者更准确地说:

Try rawurlencode. Or to be more precise:

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

此功能完全正常如何encodeURIComponent 已定义:

This function works exactly how encodeURIComponent is defined:

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

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

相关文章