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"; }
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
papéis => papel = papel anéis => anel = anel PASTEIS => PASTEL = PASTel CAMIÕES => CAMIÃO = CAMIão rodas => roda = roda cães => cão = cão meses => mês = mê vezes => vez = vez luzes => luz = luz cristais => cristal = cristal canções => canção = canção nuvens => nuvem = nuvem alemães => alemão = alemão

preferences:
125.27 ms | 408 KiB | 5 Q