在php中编码包含连字符( - )和点(.)的URL

Raj*_*PHP 12 php encode urlencode

我需要编码的URL在其中一个API中进行处理,但它需要完整的编码URL.例如,来自的URL:

http://test.site-raj.co/999999?lpp=1&px2=IjN
Run Code Online (Sandbox Code Playgroud)

必须成为编码的URL,如:

http%3a%2f%test%site%2draj%2eco%2f999999%3flpp%3d1%26px2%3dIjN
Run Code Online (Sandbox Code Playgroud)

我需要对每个符号进行编码,甚至像上面的点(.)和连字符( - )一样.

Dên*_*one 20

试试这个.在函数内部,如果你不止一次使用它...

$str = 'http://test.site.co/999999?lpp=1&p---x2=IjN';
$str = urlencode($str);
$str = str_replace('.', '%2E', $str);
$str = str_replace('-', '%2D', $str);
echo $str;
Run Code Online (Sandbox Code Playgroud)


Boa*_*ann 9

这将编码所有非普通字母或数字的字符.您仍然可以使用标准urldecode或rawurldecode对此进行解码:

function urlencodeall($x) {
    $out = '';
    for ($i = 0; isset($x[$i]); $i++) {
        $c = $x[$i];
        if (!ctype_alnum($c)) $c = '%' . sprintf('%02X', ord($c));
        $out .= $c;
    }
    return $out;
}
Run Code Online (Sandbox Code Playgroud)