1

I am receiving as a $_GET parameter a string with "6d617263f2" as hex representation.

As far as I understand character encoding, this is not an UTF-8 string. If I print it with UTF-8 encoding what I get is "marc�". If I convert the string to UTF-8 with utf8_encode I get the correct representation, which is marcò.

I setted all my character encodings (default_carset, iconv and mbstring) in the php.ini file to work with UTF-8. I also have the mbstring.encoding_translation set to On.

I'm not able to fully understand what is going on... why I am not getting my $_GET parameter encoded correctly with UTF-8?

My guesses are:

  • the client is using another character encoding and if I want to use UTF-8, there is no other way that explicitely convert my parameter to UTF-8

  • I am missing something somewhere...

Could you please help me to shed some light on this?

2
  • Are you in control of sending that GET parameter, or are you receiving it from an uncontrollable 3rd party? Commented Feb 26, 2016 at 10:57
  • @deceze uncontrollable 3rd party Commented Feb 26, 2016 at 10:58

1 Answer 1

2

If you don't control the origin of that GET parameter, then there's nothing you can do. PHP will give you the string as is and won't automatically convert its encoding. It can't, since it doesn't know what encoding to convert from. There's no spec or anything where anyone could get that information from. You need to specify what encoding you accept strings in. Don't leave it up to the client to decide, because then you have no idea what you're going to get.

If the client sends you ISO-8859 encoded text, but you want it to be UTF-8 encoded internally (a sensible choice BTW), you will simply have to convert its encoding. I'd use iconv('ISO-8859-1', 'UTF-8', $_GET['foo']) for that since it's more explicit what it does, but utf8_encode happens to do exactly the same thing.

Sign up to request clarification or add additional context in comments.

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.