urlencode在PHP中不起作用(localhost)

时间:2014-05-06 10:03:09

标签: php localhost urlencode

所以我在一个项目中工作,我需要编码我的网址,例如在我的网址中我有:

$url = 'http://www.test.com/?sms=+212&text=bla bla'
urlencode($url);

$url = urlencode($url);

我也试过了rawurlencode($url);,我得到了相同的 Url 而没有变化!!

所以,如果有人知道我会如此感激:)

4 个答案:

答案 0 :(得分:1)

$url = "http://www.test.com/?sms=+212&text=bla bla";
echo $url = urlencode($url);

答案 1 :(得分:1)

urlencode返回一个新的编码网址,不会更改传递给它的$ url。试试这个

$encoded_url = urlencode($url);

$ encoded_url就是你想要的

答案 2 :(得分:0)

第二个确实有效:

$url = "http://www.test.com/?sms=+212&text=bla bla";

$url = urlencode($url);

echo $url;

参见 demonstration

我也注意到了

$url = http://www.test.com/?sms=+212&text=bla bla

是无效的字符串格式。使用' '" "表示字符串:

$url = "http://www.test.com/?sms=+212&text=bla bla";

答案 3 :(得分:0)

Ur url数据应为字符串。

<?php
$url = 'http://www.test.com/?sms=+212&text=bla bla';
$encoded = urlencode($url);
echo(urlencode($url)); //simple urlencode
echo('<br>');
echo($encoded);//output with varible
echo('<br>');
$url = urlencode($url);
echo($url);

注意你重新定义ur url var的地方。该代码的输出如下:

http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla