Library to remove diacritic signs

Library to remove diacritic signs

For various reasons you can run into situation where you have to get a clean string without accents. It might be an input query like “chèvre ou bœuf” to be converted into “chevre ou boeuf”, or it could be a string like “côté de veau cuit à cœur” that you want to display as “cote de veau cuit a coeur”.

Wouldn’t it be nice to have a simple function in php that sanitizes stings from accent signs? Here it is https://github.com/vria/nodiacritic.

Installation gets easier if you use composer:

composer require vria/nodiacritic

Once installation is finished, you can use the function filter() of VRia\Utils\NoDiacritic class:

use VRia\Utils\NoDiacritic;

/* Chevre ou boeuf */
NoDiacritic::filter("Chèvre ou bœuf");

The library properly treats German and Danish words if you pass an appropriate locale. In fact, the function filter() has two arguments:

filter($string, $locale = null)

The first argument is a string to be cleaned, and a second one is a locale, which is a standardized language name ISO 639-1. For example:

/* Schoene strasse */
NoDiacritic::filter("Schöne straße", "de");

Moreover, there is [NoDiactiticBundle] that integrates this library into Symfony. In particular, bundle adds the nodiacritic filter to be used in twig templates:

{# Revolution francaise #}
{{ "Révolution française" | nodiacritic }}

There is no need to specify locale because bundle knows it from the current Request object. If for some reason desirable locale differs from the Request’s one there is a possibility to pass it:

{# Schoene strasse #}
{{ "Schöne straße" | nodiacritic("de") }}

I hope that these libraries will make your life esier.

NoDiactiticBundle