Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace UTF-8 characters with similar-looking ASCII characters with PHP?

I have a problem about Unicode. I need a function in PHP to convert the string:

Xin chào tất cả các bạn. Mình không biết tiếng anh.

To:

Xin chao tat ca cac ban. Minh khong biet tieng anh.

Can anybody help me?

like image 316
ns2n Avatar asked Apr 20 '14 02:04

ns2n


People also ask

Does PHP support UTF-8?

PHP | utf8_encode() FunctionThe utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8. Unicode has been developed to describe all possible characters of all languages and includes a lot of symbols with one unique number for each symbol/character.

Is UTF-8 and ASCII same?

For characters represented by the 7-bit ASCII character codes, the UTF-8 representation is exactly equivalent to ASCII, allowing transparent round trip migration. Other Unicode characters are represented in UTF-8 by sequences of up to 6 bytes, though most Western European characters require only 2 bytes3.

How did UTF-8 replace the ASCII character encoding standard?

Why did UTF-8 replace the ASCII character-encoding standard? UTF-8 can store a character in more than one byte. UTF-8 replaced the ASCII character-encoding standard because it can store a character in more than a single byte. This allowed us to represent a lot more character types, like emoji.

What is UTF-8 PHP?

Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.


1 Answers

Use iconv with the //TRANSLIT modifier:

$str1 = "Xin chào tất cả các bạn. Mình không biết tiếng anh.";
$str2 = iconv("UTF-8", "ASCII//TRANSLIT", $str1);
print($str1.PHP_EOL.$str2);

The output will be:

Xin chào tất cả các bạn. Mình không biết tiếng anh.
Xin chao tat ca cac ban. Minh khong biet tieng anh.

DEMO

like image 72
Sharanya Dutta Avatar answered Sep 28 '22 22:09

Sharanya Dutta