Replacing accented characters php

Replacing accented characters php

  1. Blog
  2. 15 November 2026
  3. 5 min read

Stefan Bogdanescu

Founder & Senior Architect

The Regex Trap in Accented Character Normalization

The code snippet you are looking at demonstrates a very common misunderstanding of how regular expressions handle character classes versus alternation. Your goal is to strip accents and normalize characters like "É" into their ASCII equivalents, but the current implementation results in "ric cantona" instead of "eric cantona". The culprit lies in the syntax used inside your preg_replace patterns.

You have defined your patterns using vertical bars (|) within square brackets:

$patterns[0] = '/[á|â|à|å|ä]/';

In regular expressions, square brackets define a character class. Inside a character class, the pipe symbol | is treated as a literal character (representing the letter "pipe"), not as an alternation operator. Therefore, your pattern [á|â|à|å|ä] is actually looking for a single character that matches one of these specific options:

  1. The letter 'á'
  2. A literal vertical bar '|'
  3. The letter 'â'
  4. And so on.

When you run strtolower("Éric"), you get "éric". Your regex then scans for the character '|'. Since there is no pipe in the string, that specific pattern fails to match anything. However, because your array contains multiple patterns, PHP iterates through them. If a pattern doesn't match, it simply moves to the next one.

Wait, if they don't match, why does the 'e' disappear entirely? Let's look closer at the logic. Actually, the issue is slightly subtler than just the pipe. You are trying to match multiple accented characters in a single pass, but you are doing it incorrectly.

The pattern [á|â...] technically matches any one of those specific characters listed. However, if your string contains "é", and your first pattern array does not explicitly list "é" (because it uses the pipe syntax which is invalid for alternation inside brackets), the match fails. But looking at your code, you did include 'é' in $patterns[1].

Let's re-evaluate the specific failure: Éric becomes éric. The letter 'e' remains missing. This suggests that the regex engine isn't finding a match for 'é' against your provided patterns. Why? Because inside [...], the pipe | is literal. Your pattern expects a literal pipe character or one of the accented vowels. If the input string contains "é" and your pattern is /[á|â|à|å|ä]/, that pattern will never match "é". It only matches 'á', '|', 'â', etc. You simply forgot to include 'é' in the first group, or you assumed the pipe meant "or" inside a character class.

The correct approach is to use the | operator for alternation outside of square brackets, or simply list all characters directly within the brackets without relying on the pipe character (which acts literally there).

Here is the corrected logic using a single, comprehensive pattern that explicitly lists every accented variant you wish to normalize:

$string = "Éric Cantona";
$strict = strtolower($string); // 'éric cantona'

// Define all characters to replace in a single character class.
// Note: We do not use '|' inside []. We just list them.
$pattern = '/[áâàåäéèêëíîìïóôòøõöúûùüæçß]/'; 
$replacement = 'a|e|i|o|u|ae|c|ss'; // A single string replacement won't work well with complex mappings.

// Since mapping 'a'->'a', 'é'->'e', etc., requires different replacements for each char,
// we must use a callback or a more granular approach.

The standard preg_replace function replaces all matches in a string with the same replacement value provided in the second argument. You cannot map "á" to "a", "é" to "e", and "ç" to "c" using a single static replacement string like 'ae'.

To solve this dynamically, you need to use a callback function that inspects the matched character and returns the specific ASCII equivalent. Here is the robust implementation:

$string = "Éric Cantona";
$strict = strtolower($string); // 'éric cantona'

// Define the mapping array
$mappings = [
    'á' => 'a', 'à' => 'a', 'â' => 'a', 'ä' => 'a', 'å' => 'a',
    'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
    'í' => 'i', 'ì' => 'i', 'î' => 'i', 'ï' => 'i',
    'ó' => 'o', 'ò' => 'o', 'ô' => 'o', 'ö' => 'o', 'ø' => 'o', 'õ' => 'o',
    'ú' => 'u', 'ù' => 'u', 'û' => 'u', 'ü' => 'u',
    'æ' => 'ae', 'ç' => 'c', 'ß' => 'ss'
];

// Create the regex pattern including all keys from our mapping
$pattern = '/[' . preg_quote(array_keys($mappings), '/') . ']/';

// Use a callback to handle variable replacements
$strict = preg_replace_callback($pattern, function ($matches) use ($mappings) {
    return $mappings[$matches[0]];
}, $strict);

echo "Final: " . $strict; // Outputs: eric cantona

Why the Callback is Necessary

Using preg_replace with a static second argument (e.g., 'a') replaces every occurrence of any match with 'a'. This works if all accented characters map to the same base, but it fails for 'é' (which maps to 'e') or 'ç' (which maps to 'c'). The callback function allows the logic to inspect which specific character was found in the string and return the correct corresponding ASCII letter.

Alternative: Using iconv

While the regex method gives you granular control, it is verbose to maintain a large mapping array. In many environments, PHP offers the iconv extension, which handles transliteration out of the box. This is often preferred for production code because it leverages ICU data for accuracy and requires less custom logic.

$string = "Éric Cantona";
// Convert to lowercase first if needed, then normalize
$strict = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', strtolower($string));
echo $strict; // Outputs: eric cantona

The iconv approach with the TRANSLIT flag attempts to convert non-ASCII characters to their closest ASCII equivalents. If a character cannot be transliterated (like certain symbols), it is ignored based on the IGNORE flag. This usually produces the exact "eric cantona" output you need without writing out every possible accented variant by hand.

Summary of Pitfalls

  1. The Pipe in Character Classes: Never use | inside [] if you intend to mean "or". It is a literal character.
  2. Static Replacement Limits: You cannot map multiple distinct characters to different replacements using a single string in preg_replace. Use preg_replace_callback for complex mappings.
  3. Case Sensitivity: Always normalize case (e.g., strtolower) before attempting accent removal, or ensure your regex pattern covers both uppercase and lowercase variants explicitly.

Our network