3

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.

3
  • 3
    "and this is not a valid url." sure it is. Why wouldn't it? Commented Nov 17, 2016 at 11:09
  • I need the empty spaces be like %20 not +. That's the only problem Commented Nov 17, 2016 at 11:19
  • Could I understand why do you want %20 not +? @IvanMoreira Commented Nov 17, 2016 at 11:21

2 Answers 2

2

You need rawurlencode

$filename = "201-SALÃO DE JOGOS.jpg";
print rawurlencode($filename);
Sign up to request clarification or add additional context in comments.

6 Comments

mb_rawurlencode is nonsense; rawurlencode works on a byte level and doesn't care about the encoding at all. In fact, the results of both functions are identical. I have no idea why that commenter thought an mb variant would be useful in any way.
@deceze: Ok. Thanks for the updates. Removed the comment.
Thanks a lot! my code now is $var = '201-SALÃO DE JOGOS.jpg'; echo rawurlencode(utf8_decode($var));
@IvanMoreira: Why you have used utf8_decode when rawurlencode is already giving you exactly want you want?
rawurlencode is not giving me exactly what i want( i don't know why). To make the string be exactly the way i want, i had to use utf8_decode
|
0

You could able to use str_replace to replace + with %20:

<?php

$var = "201-SALÃO DE JOGOS.jpg";
$output = str_replace('+','%20',urlencode($var));
echo $output;
echo "*****";
echo urldecode($output);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.