<?php function is_allcaps($string) { $last_letter = mb_substr($string, -1, 1, 'UTF-8'); return $last_letter === mb_strtoupper($last_letter, 'UTF-8'); // otherwise use cytpe_upper() and setlocale() } function plural_to_singular($string) { // quick return of "untouchables" if(preg_match('~^(?:[oó]culos|parab[eé]ns|f[eé]rias)$~iu', $string)) { return $string; } $regex_map = [ '~[õã]es$~iu' => 'ão', '~(?:[áó].*e|[eé])is$~iu' => 'el', '~[^eé]\Kis$~iu' => 'l', '~ns$~iu' => 'm', '~eses$~iu' => 'ês', '~(?:[rzs]\Ke)?s$~iu' => '' ]; foreach ($regex_map as $pattern => $replacement) { $singular = preg_replace($pattern, $replacement, $string, 1, $count); if ($count) { return is_allcaps($string) ? mb_strtoupper($singular) : $singular; } } return $string; } $words = array( 'óculos' => 'óculos', '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"; }
You have javascript disabled. You will not be able to edit any code.
Value for `_results` contains invalid data `array`