urlencode doesn't work in PHP (localhost)

2019-08-30 04:53发布

问题:

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";