urlencode doesn't work in PHP (localhost)

∥☆過路亽.° 提交于 2019-12-11 17:53:44

问题


So i worked in a project that i need to encode my URL, for example in my URL i have :

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

$url = urlencode($url);

Also i tried rawurlencode($url); and i got the same Url without changes !!

so please if someone has any idea i will be so appreciative :)


回答1:


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



回答2:


urlencode returns a new encoded url, doesn't change the $url passing to it. try this

$encoded_url = urlencode($url);

$encoded_url is what you want




回答3:


Ur url data should be string.

<?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);

Be attentive in what place u redefined ur url var. Output of that code looks like:

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



回答4:


The second one really works:

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

$url = urlencode($url);

echo $url;

See a demonstration.

I also noticed that

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

is an invalid string format. Use ' ' or " " for strings:

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


来源:https://stackoverflow.com/questions/23491575/urlencode-doesnt-work-in-php-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!