PHP 如何使用 rawurlencode 对所有字符进行编码

dav*_*rko 1 php string encode urlencode utf-8

我想知道是否有任何“开箱即用”的 php 函数可以对字符串中的所有字符进行编码,而不仅仅是空格。

$str="Encode All Characters Not Only The Spaces In Between";

echo rawurlencode($str);
Run Code Online (Sandbox Code Playgroud)

这返回:

Encode%20All%20Characters%20Not%20Only%20The%20Spaces%20In%20Between
Run Code Online (Sandbox Code Playgroud)

但我想对所有字符串进行编码,而不仅仅是空格。

geo*_*org 5

没有这样的东西,但很容易写:

\n\n
function encode_all($str) {\n    $hex = unpack('H*', $str);\n    return preg_replace('~..~', '%$0', strtoupper($hex[1]));\n}\n\n$str = 'big \xc6\x92\xc3\xbc\xc3\x9fchen';\nprint_r(encode_all($str));\n
Run Code Online (Sandbox Code Playgroud)\n