Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best method of converting user input to UTF-8

I'm building a PHP web application, and it works in UTF-8. The database is UTF-8, the pages are served as UTF-8 and I set the charset using a meta tag to UTF-8. Of course, with users using Internet Explorer, and copying & pasting from Microsoft Office, I somehow manage to get not UTF-8 input occasionally.

The ideal solution would be to throw an HTTP 400 Bad Request error, but obviously I can't do that. The next best thing is converting $_GET, $_POST and $_REQUEST to UTF-8. Is there anyway to see what character encoding the input is in so I can pass it off to iconv? If not, what's the best solution for doing this?

like image 391
Brandon Wamboldt Avatar asked Jul 26 '12 14:07

Brandon Wamboldt


1 Answers

Check out mb_detect_encoding() Example:

$utf8 = iconv(mb_detect_encoding($input), 'UTF-8', $input);

There's also utf8_encode() if you guarantee that the string is input as ISO-8859-1.

like image 69
Jeff Lambert Avatar answered Sep 18 '22 12:09

Jeff Lambert