The Hidden Trap in PHP Character Replacement: Why Your preg_replace Fails on User Input
You are encountering a classic case of "works on my machine" versus "works on production," but in this scenario, it works on a hardcoded string and fails on user input. The root cause isn't that your regex logic is flawed; it is that the data you are trying to manipulate has already been transformed by PHP's default encoding handling before your function even sees it.
When you run $chain = preg_replace($pattern, $replace, $chain); on a literal string like "prêt-à -porter", the characters are stored exactly as they appear in your source code. The regex engine finds the accented bytes and swaps them for ASCII equivalents. However, when processing $_POST['first_name'], the behavior depends entirely on how PHP decoded that incoming HTTP request.
The Encoding Mismatch
PHP's default internal encoding is typically UTF-8. When an HTTP request arrives with a Content-Type header of text/html; charset=utf-8, PHP automatically decodes the raw byte stream into Unicode characters using the UTF-8 mapping table.
If your database connection or form submission sends data where the accented character (like à ) is represented by two bytes (e.g., 0xC3 0xA0), PHP interprets this correctly as a single logical character à . Your regex pattern looks for this specific multi-byte sequence. The replacement works fine on $chain because that variable holds the literal string defined in your script, which matches the expected input.
The issue arises if the incoming data from your form or database is already stored as "pre-composed" characters (where the accent and base letter are merged) versus "decomposed" sequences, or if there is a mismatch between the encoding declared in the HTTP header and what PHP expects. More commonly, however, the problem is simpler: you are trying to replace characters that might not exist in the specific encoding of the input string.
If your database stores data as UTF-8 but your PHP script runs in an environment where mbstring.internal_encoding was set to ISO-8859-1 (a legacy common occurrence), PHP will interpret the incoming bytes differently. A byte sequence representing à in UTF-8 might be interpreted as two separate characters in ISO-8859-1, causing your regex pattern for a single multi-byte character to fail to match anything.
The strtr Red Herring
The second example you provided using strtr highlights another subtle pitfall: data persistence and state.
echo $first_name; // print áááááábéééééébšššš
$trans = array("á" => "a", "é" => "e", "š" => "s");
echo strtr("áááááábéééééébšššš", $trans); // print aaaaaabeeeeeebssss
echo strtr($first_name,$trans); // print áááááábéééééébšššš
This code snippet demonstrates that strtr behaves exactly as expected when given a literal string, yet fails on $first_name. This confirms that the variable $first_name does not contain the characters your array expects.
If you are seeing output like áááááábéééééébšššš in your browser or log, but your replacement logic doesn't touch it, check your database connection settings. If your MySQL server is configured to send data with a different character set than what PHP expects (e.g., DB is UTF-8, PHP expects ISO), the characters arrive at PHP already "decoded" as whatever PHP thinks they are. If PHP thinks à is actually composed of two characters, your regex looking for one complex unit won't find a match.
The Correct Approach: Normalize Before Replace
The most robust solution to this problem is not to build massive arrays of every possible accented character. Instead, you should normalize the input string before attempting replacement.
Use the mb_convert_case function in combination with normalizer_form_canonical_compact (or simply rely on mb_convert_encoding to ensure strict UTF-8) to handle the normalization. However, the most direct fix for stripping accents is using mb_strtolower or a custom transliteration library that handles Unicode decomposition.
A reliable pattern involves decomposing the characters first. In modern PHP 5.6+, you can use normalizer_form_canonical_decomposition combined with regex to remove combining diacritics, but a simpler, dependency-free approach for stripping accents is often achieved by converting the string to its decomposed form and then removing the diacritical marks.
Here is a more robust implementation that handles encoding inconsistencies:
function stripAccents($string) {
// Ensure the string is treated as UTF-8 first
$str = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
// This regex matches combining diacritical marks and removes them
// It works on both decomposed (a + combining acute) and precomposed (á) forms
return preg_replace('/\pM/u', '', $str);
}
// Usage
$first_name = $_POST['first_name'];
$clean_name = stripAccents($first_name);
echo $clean_name; // Will now correctly print 'aaaaabeeeeeebsss' regardless of input variation
Why This Works Better
The \pM property in the regex matches any character that is a combining diacritical mark (like the acute accent Ì on its own). Unicode normalizes characters into different forms. Even if your database stores á as a single codepoint, it can be internally represented or processed in ways that expose the underlying structure. By explicitly removing these combining marks, you ensure that whether the input is pre-composed (á) or decomposed (a + accent), the result is always the base ASCII character.
This approach eliminates the need to manually list every accented variant (like à , á, ä, etc.) in your replacement arrays, reducing the risk of missing an edge case and making your code maintainable for future needs like handling German à or French Å. Always validate that your database connection charset matches your application's expected encoding before processing user input.
