How can I convert accented characters to their base characters in PHP?

Mary-Kate Olsen
Release: 2024-11-01 08:32:02
Original
240 people have browsed it

How can I convert accented characters to their base characters in PHP?

Converting Accented Characters to Base Characters in PHP

The PHP language provides functionality to replace accented characters with their base counterparts.

Using Normalizer Class

The Normalizer class is designed for character normalization, including accents and other diacritics. To remove accents using this class:

<code class="php">use Normalizer;
$string = Normalizer::normalize($string, Normalizer::FORM_D);</code>
Copy after login

Custom Accented Character Replacement

If you do not wish to use the Normalizer class or need to customize the accent replacement, you can use the following function:

Code:

<code class="php">function unaccent($string)
{
    return preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '', htmlentities($string, ENT_QUOTES, 'UTF-8'));
}</code>
Copy after login

Example Usage:

To replace "ã" with "a" and "é" with "e":

<code class="php">$string = "ãé";
$unaccentedString = unaccent($string);</code>
Copy after login

How it Works:

This function converts the accented characters into their HTML entity representations using htmlentities(). The regular expression then replaces the HTML entity with the base character.

This method is compatible with most common accents, but it is not exhaustive and may not cover all possible cases.

The above is the detailed content of How can I convert accented characters to their base characters in PHP?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!