Strategy for supporting unicode & multi language in PHP5

前端 未结 4 611
北恋
北恋 2020-12-09 14:07

I have heard that PHP6 will natively support unicode, which will hopefully make multi-language support much easier. However, PHP5 has pretty weak support for unicode and mul

4条回答
  •  时光取名叫无心
    2020-12-09 14:31

    It's not all that hard really, but you may want to make your question a bit more specific.

    If you're talking to a database, make sure your database stores data in UTF-8 and the connection to your database is in UTF-8 (a common pitfall). Make sure to run this when establishing a connection:

    mysql_set_charset('utf8');
    

    For user input, set the accept-charset attribute on your forms.

    Serve your sites with an appropriate HTTP header:

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

    or at least set appropriate meta tags for your site:

    
    

    Keep your source code files encoded in UTF-8.

    If you keep everything in UTF-8, you usually don't need to worry about anything. It's only getting problematic once you start mixing encodings throughout your app.

    If you're starting to talk about string manipulation of course, you'll have to take a little more care. Mostly you'll want to use the mb_ set of string functions, as you pointed out yourself.

提交回复
热议问题