Does PHP have any standard function(s) to convert Unicode开发者_开发技巧 strings to plain, good old-fashioned ANSI strings (or whatever format PHP's htmlentities
understands?
Is there any function that converts UTF-8 strings to HTML that can be understood by the most popular browsers?
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.
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.)
Browsers already understand UTF-8. If you want them to know that you're sending them UTF-8 then you need to tell them.
精彩评论