Reputation: 2656
I have an url with spaces:
http://mihalko.eu/image/data/ister/Nomad Plus-G- cierno cervena.jpg
(browser can open this link) What is the best way to urlencode 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 can't open this link)
urlencode give me this result, but my browser wont't open this url.
Upvotes: 4
Views: 2228
Reputation: 281
You could search for the last "/" and use urlencode for last part of your string, like that:
$pos=strrpos($url,"/")+1;
$newurl=substr($url,0,$pos) . rawurlencode(substr($url,$pos));
If your only problems are spaces, then you could use
str_replace(" ","%20",$url);
Upvotes: 0
Reputation: 141829
Use rawurlencode:
echo rawurlencode('Nomad Plus-G- cierno cervena.jpg');
// Nomad%20Plus-G-%20cierno%20cervena.jpg
If you're okay with spaces being encoded as +
then just use urlencode instead.
Upvotes: 2