4

I am getting data from various site through url. Url parameters are url-encoded with the php urlencode() function, but character encoding can be still be UTF-8 or Latin-1.

For example, the é character, when url-encoded from UTF-8 becomes %C3%A9 but when url-encoded from Latin-1, it becomes %E9.

When I get data through url, I use urldecode() and then I need to know what is the character encoding so I eventually use utf8_encode before I insert them in a MySQL database.

Strangely, the following code doesn't work :

$x1 = 'Cl%C3%A9ment';
$x2 = 'Cl%E9ment';

echo mb_detect_encoding(urldecode($x1)).' / '.mb_detect_encoding(urldecode($x2));

It returns UTF-8 / UTF-8

Why is that, what am I doing wrong and how can I know the character encoding of those string ?

Thanks

3
  • If you are using $_GET you should not use urldecode() as the values are already decoded.
    – jeroen
    Jan 27, 2014 at 15:05
  • @jeroen - Most likely, it's just a trick to create a simple test case. Jan 27, 2014 at 15:11
  • @ÁlvaroG.Vicario The example is clear, that's why it's just a comment :-)
    – jeroen
    Jan 27, 2014 at 15:14

1 Answer 1

3

mb_detect_encoding() is normally useless with the default second parameter:

<?php

$x1 = 'Cl%C3%A9ment';
$x2 = 'Cl%E9ment';

$encoding_list = array('utf-8', 'iso-8859-1');

var_dump(
    mb_detect_encoding(urldecode($x1), $encoding_list),
    mb_detect_encoding(urldecode($x2), $encoding_list)
);

... prints:

string(5) "UTF-8"
string(10) "ISO-8859-1"
2
  • It's working, thanks ! Does that mean that I need to know in advance what the encoding could have been used ? Isn't there a simple way to detect any existing character encoding ?
    – clemlatz
    Jan 27, 2014 at 15:47
  • 1
    It's a URL: it should be UTF-8 all the time, except in really legacy and misconfigured browsers where it can be ISO-8859-1. I don't think there're other possibilities. And no, there's no simple and reliable way to detect the encoding of a text among the hundreds of encodings that exist. Jan 27, 2014 at 15:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.