PHP: Converting Unicode strings to ANSI strings

后端 未结 3 1761
庸人自扰
庸人自扰 2020-12-18 12:02

Does PHP have any standard function(s) to convert Unicode strings to plain, good old-fashioned ANSI strings (or whatever format PHP\'s htmlentities understan

相关标签:
3条回答
  • 2020-12-18 12:35

    This can't work properly. Stored with Unicode there are many more Characters than with ANSI. So if you "convert" to ANSI, you will loose lots of charackters.

    http://php.net/manual/en/function.htmlentities.php

    You can use Unicode (UTF-8) charset with htmlentities:

    string htmlentities ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

    htmlentities($myString, ENT_COMPAT, "UTF-8"); should work.

    0 讨论(0)
  • 2020-12-18 12:35

    Browsers already understand UTF-8. If you want them to know that you're sending them UTF-8 then you need to tell them.

    0 讨论(0)
  • 2020-12-18 12:44

    Whilst I'd really recommend keeping everything in UTF-8 (as per my comment on the question), you can use the mb_convert_encoding function to convert any known UTF-8 string to US-ASCII as such:

    $asciiString = mb_convert_encoding ($sourceString, 'US-ASCII', 'UTF-8');
    

    However, this may not be a lossless conversion depending on the source character string. (Characters such as "é" will simply disappear into the void.)

    0 讨论(0)
提交回复
热议问题