Home » Php » Convert ASCII TO UTF-8 Encoding

Convert ASCII TO UTF-8 Encoding

Posted by: admin November 30, 2021 Leave a comment


How to convert ASCII encoding to UTF8 in PHP


ASCII is a subset of UTF-8, so if a document is ASCII then it is already UTF-8.


If you know for sure that your current encoding is pure ASCII, then you don’t have to do anything because ASCII is already a valid UTF-8.

But if you still want to convert, just to be sure that its UTF-8, then you can use iconv

$string = iconv('ASCII', 'UTF-8//IGNORE', $string);

The IGNORE will discard any invalid characters just in case some were not valid ASCII.


Use mb_convert_encoding to convert an ASCII to UTF-8. More info here

$string = "chárêctërs";
print(mb_detect_encoding ($string));

$string = mb_convert_encoding($string, "UTF-8");
print(mb_detect_encoding ($string));


“ASCII is a subset of UTF-8, so…” – so UTF-8 is a set? 🙂

In other words: any string build with code points from x00 to x7F has indistinguishable representations (byte sequences) in ASCII and UTF-8. Converting such string is pointless.


Use utf8_encode()

Man page can be found here http://php.net/manual/en/function.utf8-encode.php

Also read this article from Joel on Software. It provides an excellent explanation if what Unicode is and how it works. http://www.joelonsoftware.com/articles/Unicode.html