UTF-8 all the way through

2020-01-22 07:56发布

I'm setting up a new server and want to support UTF-8 fully in my web application. I have tried this in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.

Where exactly do I need to set the encoding/charsets? I'm aware that I need to configure Apache, MySQL, and PHP to do this — is there some standard checklist I can follow, or perhaps troubleshoot where the mismatches occur?

This is for a new Linux server, running MySQL 5, PHP, 5 and Apache 2.

15条回答
该账号已被封号
2楼-- · 2020-01-22 08:06

if you want a mysql solution, I had similar issues with 2 of my projects, after a server migration. After searching and trying a lot of solutions i came across with this one /nothing before this one worked):

mysqli_set_charset($con,"utf8");

After adding this line to my config file everything works fine!

I found this solution https://www.w3schools.com/PHP/func_mysqli_set_charset.asp when i was looking to solve a insert from html query

good luck!

查看更多
Root(大扎)
3楼-- · 2020-01-22 08:08

First of all if you are in < 5.3PHP then no. You've got a ton of problems to tackle.

I am surprised that none has mentioned the intl library, the one that has good support for unicode, graphemes, string operations , localisation and many more, see below.

I will quote some information about unicode support in PHP by Elizabeth Smith's slides at PHPBenelux'14

INTL

Good:

  • Wrapper around ICU library
  • Standardised locales, set locale per script
  • Number formatting
  • Currency formatting
  • Message formatting (replaces gettext)
  • Calendars, dates, timezone and time
  • Transliterator
  • Spoofchecker
  • Resource bundles
  • Convertors
  • IDN support
  • Graphemes
  • Collation
  • Iterators

Bad:

  • Does not support zend_multibite
  • Does not support HTTP input output conversion
  • Does not support function overloading

mb_string

  • Enables zend_multibyte support
  • Supports transparent HTTP in/out encoding
  • Provides some wrappers for funtionallity such as strtoupper

ICONV

  • Primary for charset conversion
  • Output buffer handler
  • mime encoding functionality
  • conversion
  • some string helpers (len, substr, strpos, strrpos)
  • Stream Filter stream_filter_append($fp, 'convert.iconv.ISO-2022-JP/EUC-JP')

DATABASES

  • mysql: Charset and collation on tables and on connection (not the collation). Also don't use mysql - msqli or PDO
  • postgresql: pg_set_client_encoding
  • sqlite(3): Make sure it was compiled with unicode and intl support

Some other Gotchas

  • You cannot use unicode filenames with PHP and windows unless you use a 3rd part extension.
  • Send everything in ASCII if you are using exec, proc_open and other command line calls
  • Plain text is not plain text, files have encodings
  • You can convert files on the fly with the iconv filter

I ll update this answer in case things change features added and so on.

查看更多
Ridiculous、
4楼-- · 2020-01-22 08:11

I'd like to add one thing to chazomaticus' excellent answer:

Don't forget the META tag either (like this, or the HTML4 or XHTML version of it):

<meta charset="utf-8">

That seems trivial, but IE7 has given me problems with that before.

I was doing everything right; the database, database connection and Content-Type HTTP header were all set to UTF-8, and it worked fine in all other browsers, but Internet Explorer still insisted on using the "Western European" encoding.

It turned out the page was missing the META tag. Adding that solved the problem.

Edit:

The W3C actually has a rather large section dedicated to I18N. They have a number of articles related to this issue – describing the HTTP, (X)HTML and CSS side of things:

They recommend using both the HTTP header and HTML meta tag (or XML declaration in case of XHTML served as XML).

查看更多
Bombasti
5楼-- · 2020-01-22 08:15

I recently discovered that using strtolower() can cause issues where the data is truncated after a special character.

The solution was to use

mb_strtolower($string, 'UTF-8');

mb_ uses MultiByte. It supports more characters but in general is a little slower.

查看更多
▲ chillily
6楼-- · 2020-01-22 08:17

In addition to setting default_charset in php.ini, you can send the correct charset using header() from within your code, before any output:

header('Content-Type: text/html; charset=utf-8');

Working with Unicode in PHP is easy as long as you realize that most of the string functions don't work with Unicode, and some might mangle strings completely. PHP considers "characters" to be 1 byte long. Sometimes this is okay (for example, explode() only looks for a byte sequence and uses it as a separator -- so it doesn't matter what actual characters you look for). But other times, when the function is actually designed to work on characters, PHP has no idea that your text has multi-byte characters that are found with Unicode.

A good library to check into is phputf8. This rewrites all of the "bad" functions so you can safely work on UTF8 strings. There are extensions like the mbstring extension that try to do this for you, too, but I prefer using the library because it's more portable (but I write mass-market products, so that's important for me). But phputf8 can use mbstring behind the scenes, anyway, to increase performance.

查看更多
Fickle 薄情
7楼-- · 2020-01-22 08:18

In PHP, you'll need to either use the multibyte functions, or turn on mbstring.func_overload. That way things like strlen will work if you have characters that take more than one byte.

You'll also need to identify the character set of your responses. You can either use AddDefaultCharset, as above, or write PHP code that returns the header. (Or you can add a META tag to your HTML documents.)

查看更多
登录 后发表回答