Ivan Moreira Ivan Moreira - 23 days ago 8
HTML Question

How to properly url encode a string

I'm trying to parse a file name to a

url
string.

the file name is:

201-SALÃO DE JOGOS.jpg


I need the output be exactly this:

201-SAL%c3O%20DE%20JOGOS.jpg


I'm trying like this:

$var = 201-SALÃO DE JOGOS.jpg;
echo urlencode($var);


But instead it returns:

201-SAL%C3%83O+DE+JOGOS.jpg


This is not a valid url. I've already tried with
htmlspecialchars()
and
htmlentities()
but these do not work.

Answer

You need rawurlencode

$filename = "201-SALÃO DE JOGOS.jpg";
print rawurlencode($filename);