PHP: Converting Unicode strings to ANSI strings

39,783

Solution 1

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.

Solution 2

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.)

Solution 3

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

Share:
39,783
pyon
Author by

pyon

Nightingale is top tier waifu. Don't agree? Fight me!

Updated on June 28, 2020

Comments

  • pyon
    pyon almost 4 years

    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?