Converting Umlauts to ASCII Equivalents in UTF-8 Strings
Problem:
Replace special characters, such as umlauts, in a UTF-8 string with their closest 7-bit ASCII equivalent. For instance, "lärm" should become "larm" and "andré" should become "andre."
Solution:
Using utf8_decode() and strtr(), as demonstrated in the given code snippet, is a common solution. However, if your source file is saved in UTF-8 and you cannot input ISO-8859-15 characters, a more elegant approach is available.
The iconv() function can be used to perform character set conversion. By specifying "ascii//TRANSLIT" as the target character set, umlauts will be automatically converted to their ASCII equivalents:
echo iconv("utf-8","ascii//TRANSLIT",$input);
Extended Example:
To illustrate the use of iconv(), consider the following code:
$input = "lärm andré"; $output = iconv("utf-8","ascii//TRANSLIT",$input); echo $output; // Output: larm andre
This code converts all umlauts in the input string to their ASCII equivalents.
The above is the detailed content of How to Replace Umlauts with ASCII Equivalents in UTF-8 Strings?. For more information, please follow other related articles on the PHP Chinese website!