Adrian
Adrian

Reputation: 2656

Url Encoding in PHP/HTML

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

Answers (3)

Adrian
Adrian

Reputation: 2656

str_replace('%2F','/',rawurlencode('url')); This is working.

Upvotes: 0

Tornado
Tornado

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

Paul
Paul

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

Related Questions