Change String name with preg_replace delete espace accent and dots but allow dot extension

Viewed 36

I have a code its working deleting dots and allow dot extension but the accent looks like:

Sin título => Sin_tÃ_tulo

$new_name = preg_replace("/\W(?=.*\.[^.]*$)/", "_", $string);

I want to delete accent or change for underscore

1 Answers

Two propositions using intl (if this module is missing in your config, install it):

$tls = Transliterator::createFromRules("::Latin-ASCII; [^A-Za-z0-9] > '_' ");
echo $tls->transliterate("Sin título"); // Sin_titulo

or:

$tls = Transliterator::createFromRules("::NFD; [^A-Za-z0-9] > '_' ");
echo $tls->transliterate("Sin título"); // Sin_ti_tulo
Related