在PHP字符串中,如何将非字母数字字符转换为十六进制的ascii


in PHP string, how to convert non-alphanumeric characters to ascii in hex

例如,如何编写一个PHP函数来转换

http://abc.com/hi-1?source=google

转换为

3

http % % 2 f % 2 abc.com % 2 fhi-1 % 3 fsource % 3 dgoogle

注:一些异常不应该被转换,比如'。' '——'

谢谢。

http://php.net/manual/en/function.urlencode.php

http://www.php.net/manual/en/function.rawurlencode.php

<?php
$string = "http://abc.com/hi-1?source=google";
echo urlencode($string); //echo rawurlencode($string);

http://php.net/manual/en/function.rawurlencode.php

echo rawurlencode("http://abc.com/hi-1?source=google");