Preparing PHP application to use with UTF-8

前端 未结 5 994
渐次进展
渐次进展 2020-12-05 03:28

UTF-8 is de facto standard for web applications now, but PHP this is not a default encoding for PHP (until 6.0). Most of the server is set up for the ISO-8859-1 encoding by

5条回答
  •  醉酒成梦
    2020-12-05 04:00

    1. All your files have to be saved in UTF-8 (without BOM) using your code editor.
    2. Webserver may be configured to send inappropriate headers, so it's recommended to override them in application level. For instance:

      header('Content-Type: text/html; charset=utf-8');
      
    3. Add HTML meta content-type:

      
      
    4. Use htmlspecialchars() instead of htmlentities() because the former is enough in utf-8 and the latter is incompatible with utf-8 by default.

    5. Tend not to use PHP standard string functions because many of them are incompatible with utf-8. Try to find their counterparts in Multibyte String or other libraries. (Don't forget to set default charset for the library before using it because the library supports many encodings and utf-8 is just one of them.)
    6. For regular expressions use u modifier. For example:

      preg_match('/ž{3,5}/u', $string, $matches);
      

      Together this is the most reliable way to check if the given string is valid utf-8 string:

      if (@preg_match('//u', $string) === false) {
          // NOT valid!
      } else {
          // Valid!
      }
      
    7. If you use the database then always set appropriate connection encoding right after the connection is made. Example for MySQL:

      mysql_set_charset('utf8', $link);
      

      Also check if columns in the database are in utf-8. It's not always needed but recomended.

提交回复
热议问题