3v4l.org

run code in 300+ PHP versions simultaneously
<?php function plural_to_singular($string) { // quick return of "un-convertibles" if(preg_match('~^(?:[oó]culos|parab[eé]ns|f[eé]rias)$~iu', $string)) { return $string; } $regex_config = [ '~[õã]es$~iu' => 'ão', '~[áó].*eis$~iu' => 'el', '~[eé]is$~iu' => 'el', '~[^eé]\Kis$~iu' => 'l', '~ns$~iu' => 'm', '~eses$~iu' => 'ês', '~[rzs]\Kes$~iu' => '', '~s$~iu' => '' ]; return preg_replace(array_keys($regex_config), $regex_config, $string, 1); } $words = array( 'papéis' => 'papel', 'anéis' => 'anel', 'PASTEIS' => 'PASTEL', 'CAMIÕES' => 'CAMIÃO', 'rodas' => 'roda', 'cães' => 'cão', 'meses' => 'mês', 'vezes' => 'vez', 'luzes' => 'luz', 'cristais' => 'cristal', 'canções' => 'canção', 'nuvens' => 'nuvem', 'alemães' => 'alemão' ); foreach($words as $plural => $singular) { echo "$plural => $singular = " , plural_to_singular($plural) , "\n"; }

preferences:
24.46 ms | 406 KiB | 5 Q