Home > Backend Development > PHP Tutorial > How to Replace Umlauts with ASCII Equivalents in UTF-8 Strings?

How to Replace Umlauts with ASCII Equivalents in UTF-8 Strings?

DDD
Release: 2024-11-29 19:46:14
Original
352 people have browsed it

How to Replace Umlauts with ASCII Equivalents in UTF-8 Strings?

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);
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template