Home >Backend Development >PHP Tutorial >How to Properly Replace Accented Characters with Their Unaccented Counterparts in PHP?

How to Properly Replace Accented Characters with Their Unaccented Counterparts in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-12-09 14:15:11238browse

How to Properly Replace Accented Characters with Their Unaccented Counterparts in PHP?

Replacing Accented Characters in PHP

In PHP, replacing accented characters with their regular counterparts can be a challenging task. Consider the following example:

$string = "Éric Cantona";
$strict = strtolower($string);

$patterns = [
    '/[á|â|à|å|ä]/',
    '/[ð|é|ê|è|ë]/',
    '/[í|îì|ï]/',
    '/[ó|ô|ò|ø|õ|ö]/',
    '/[ú|û|ù|ü]/',
    '/æ/',
    '/ç/',
    '/ß/'
];

$replacements = [
    'a',
    'e',
    'i',
    'o',
    'u',
    'ae',
    'c',
    'ss'
];

$strict = preg_replace($patterns, $replacements, $strict);
echo "Final: ".$strict;

This code aims to replace accented characters in the string "Éric Cantona" with their unaccented equivalents, but the output is "ric cantona," which is incorrect. The issue lies in the fact that the code doesn't account for uppercase accented characters like "É" in "Éric."

The correct approach is to use a more comprehensive array of unwanted characters and their replacements, as seen below:

$unwanted_array = [
    'Š' => 'S', 'š' => 's',
    'Ž' => 'Z', 'ž' => 'z',
    'À' => 'A', 'Á' => 'A',
    'Â' => 'A', 'Ã' => 'A',
    'Ä' => 'A', 'Å' => 'A',
    'Æ' => 'A', 'Ç' => 'C',
    'È' => 'E', 'É' => 'E',
    'Ê' => 'E', 'Ë' => 'E',
    'Ì' => 'I', 'Í' => 'I',
    'Î' => 'I', 'Ï' => 'I',
    'Ñ' => 'N', 'Ò' => 'O',
    'Ó' => 'O', 'Ô' => 'O',
    'Õ' => 'O', 'Ö' => 'O',
    'Ø' => 'O', 'Ù' => 'U',
    'Ú' => 'U', 'Û' => 'U',
    'Ü' => 'U', 'Ý' => 'Y',
    'Þ' => 'B', 'ß' => 'Ss',
    'à' => 'a', 'á' => 'a',
    'â' => 'a', 'ã' => 'a',
    'ä' => 'a', 'å' => 'a',
    'æ' => 'a', 'ç' => 'c',
    'è' => 'e', 'é' => 'e',
    'ê' => 'e', 'ë' => 'e',
    'ì' => 'i', 'í' => 'i',
    'î' => 'i', 'ï' => 'i',
    'ð' => 'o', 'ñ' => 'n',
    'ò' => 'o', 'ó' => 'o',
    'ô' => 'o', 'õ' => 'o',
    'ö' => 'o', 'ø' => 'o',
    'ù' => 'u', 'ú' => 'u',
    'û' => 'u', 'ý' => 'y',
    'þ' => 'b', 'ÿ' => 'y'
];
$str = strtr($str, $unwanted_array);

By using this array, the code will accurately replace both lowercase and uppercase accented characters.

The above is the detailed content of How to Properly Replace Accented Characters with Their Unaccented Counterparts in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn