避免多个url_encode()

时间:2014-04-30 11:55:58

标签: php urlencode

如何避免多个url_encode

echo urlencode('http://www.php.net/urlencode?url=http%3A%2F%2Fwww.php.net%2Furlencode')

输出

http%3A%2F%2Fwww.php.net%2Furlencode%3Furl%3Dhttp%253A%252F%252Fwww.php.net%252Furlencode

预期结果

http%3A%2F%2Fwww.php.net%2Furlencode%3Furl%3Dhttp%3A%2F%2Fwww.php.net%2Furlencode

2 个答案:

答案 0 :(得分:1)

假设您想要urlencode在查询字符串中使用的URL(递归/迭代),如下所示:

$s = 'http://www.php.net/urlencode?url=http%3A%2F%2Fwww.php.net%2Furlencode';
//parse the url
$p = parse_url($s);
//check if there is a query string 
$q = isset($p['query']) ? $p['query'] : '';
//urlencode the main url and then append the already encoded query string
echo urlencode(str_replace($q, '', $s)) . $q;

或者可能:

echo urlencode(urldecode($s));

答案 1 :(得分:-3)

str_replace("%25","",urlencode('http://www.php.net/urlencode?url=http%3A%2F%2Fwww.php.net%2Furlencode'))