URL Encoding in PHP / HTML

I have a url with spaces:

http://mihalko.eu/image/data/ister/Nomad Plus-G-cierno cervena.jpg

(browser can open this link) What is the best urlencode way for this URL (space =% 20)?

Nomad% 20Plus-G-% 20cierno% 20cervena.jpg

UrlEncode:

HTTP% 3A% 2F% 2Fmihalko.eu% 2Fimage% 2Fdata% 2Fister% 2FNomad + Plus-G- + cierno + cervena.jpg

(browser cannot open this link)
urlencode give me this result but my browser will not open this url.

+4
source share
3 answers

Use rawurlencode :

echo rawurlencode('Nomad Plus-G- cierno cervena.jpg'); // Nomad%20Plus-G-%20cierno%20cervena.jpg 

If you're ok with spaces encoded as + , then just use urlencode .

+2
source

You can find the last "/" and use urlencode for the last part of your line, for example:

 $pos=strrpos($url,"/")+1; $newurl=substr($url,0,$pos) . rawurlencode(substr($url,$pos)); 

If your only problems are spaces, you can use

 str_replace(" ","%20",$url); 
0
source

str_replace ('% 2F', '/', rawurlencode ('URL')); It works.

0
source

Source: https://habr.com/ru/post/1416636/


All Articles