How to properly handle international character in PHP / MySQL / Apache

后端 未结 5 1889
我在风中等你
我在风中等你 2021-01-03 14:47

I need to create an application in PHP that can handle all Unicode characters in all places — edit fields, static HTML, database. Can somebody tell me the complete list of a

5条回答
  •  遥遥无期
    2021-01-03 15:28

    Apache

    The server encoding must be either not set, or set to UTF-8. This is done via the apache AddDefaultCharset directive. This can go to the virtualhost or the general file (see documentation).

    AddDefaultCharset utf-8
    

    MySql

    • Set the collation of the database to be UTF-8
    • Set the connection encoding. It can be done as someone said with mysqli_set_charset, or by sending this just after connecting:
        SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'
    

    PHP

    1- You should set the HTML charset of the page to be UTF-8, via a meta tag on the page, or via a PHP header:

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

    2- You should always use the mb* version of string-related functions, for example, mbstrlen instead of strlen to get the string length of a string.

    This should allow you to have UTF-8 everywhere, from the pages to the data. A test you can do: right-click anywhere on the page using firefox, and select Show page information. The effective encoding is listed in that page.

提交回复
热议问题