Convert accented Unicode characters to non-accented in PHP
A guide on how to convert accented Unicode characters in the Vietnamese alphabet to non-accented letters using PHP. This PHP code efficiently handles Vietnamese text processing.
<?php
function remove_accents($string) {
// Array to convert accented characters to non-accented
$accents = array(
'à' => 'a', 'á' => 'a', 'ạ' => 'a', 'ả' => 'a', 'ã' => 'a', 'ă' => 'a', 'ằ' => 'a', 'ắ' => 'a', 'ẵ' => 'a', 'ẳ' => 'a', 'ặ' => 'a',
'â' => 'a', 'ầ' => 'a', 'ấ' => 'a', 'ẫ' => 'a', 'ẩ' => 'a', 'ậ' => 'a',
'è' => 'e', 'é' => 'e', 'ẹ' => 'e', 'ẻ' => 'e', 'ẽ' => 'e', 'ê' => 'e', 'ề' => 'e', 'ế' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e',
'ì' => 'i', 'í' => 'i', 'ị' => 'i', 'ỉ' => 'i', 'ĩ' => 'i',
'ò' => 'o', 'ó' => 'o', 'ọ' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ô' => 'o', 'ồ' => 'o', 'ố' => 'o', 'ỗ' => 'o', 'ổ' => 'o', 'ộ' => 'o',
'ơ' => 'o', 'ờ' => 'o', 'ớ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o',
'ù' => 'u', 'ú' => 'u', 'ụ' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ư' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u',
'ỳ' => 'y', 'ý' => 'y', 'ỵ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y',
'đ' => 'd',
'À' => 'A', 'Á' => 'A', 'Ạ' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ă' => 'A', 'Ằ' => 'A', 'Ắ' => 'A', 'Ẵ' => 'A', 'Ẳ' => 'A', 'Ặ' => 'A',
'Â' => 'A', 'Ầ' => 'A', 'Ấ' => 'A', 'Ẫ' => 'A', 'Ẩ' => 'A', 'Ậ' => 'A',
'È' => 'E', 'É' => 'E', 'Ẹ' => 'E', 'Ẻ' => 'E', 'Ẽ' => 'E', 'Ê' => 'E', 'Ề' => 'E', 'Ế' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E',
'Ì' => 'I', 'Í' => 'I', 'Ị' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I',
'Ò' => 'O', 'Ó' => 'O', 'Ọ' => 'O', 'Ỏ' => 'O', 'Õ' => 'O', 'Ô' => 'O', 'Ồ' => 'O', 'Ố' => 'O', 'Ỗ' => 'O', 'Ổ' => 'O', 'Ộ' => 'O',
'Ơ' => 'O', 'Ờ' => 'O', 'Ớ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O',
'Ù' => 'U', 'Ú' => 'U', 'Ụ' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ư' => 'U', 'Ừ' => 'U', 'Ứ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U',
'Ỳ' => 'Y', 'Ý' => 'Y', 'Ỵ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y',
'Đ' => 'D'
);
// Replace accented characters with non-accented characters
return strtr($string, $accents);
}
// Example usage
$text = "Đây là ví dụ về chữ cái có dấu: ắ, à, ê, ơ, đ.";
$no_accent_text = remove_accents($text);
echo $no_accent_text;
?>
Detailed explanation:
-
remove_accents
function:- The
$accents
array maps accented Unicode characters to their non-accented equivalents. - The
strtr
function replaces accented characters with non-accented characters based on the$accents
array.
- The
-
Function usage:
- When passing a string with accented characters, the function returns the equivalent string without accents.
-
Example:
- The string
"Đây là ví dụ về chữ cái có dấu: ắ, à, ê, ơ, đ."
is converted to"Day la vi du ve chu cai co dau: a, a, e, o, d."
.
- The string
PHP Version:
This code can run on PHP versions from 5.3 and above, as the strtr
function and Unicode characters are supported in these versions.