I came across a small issue to display the singular version of any English word using some grammar rules to de-pluralise a word.
function depluralize($word){ // Here is the list of rules. To add a scenario, // Add the plural ending as the key and the singular // ending as the value for that key. This could be // turned into a preg_replace and probably will be // eventually, but for now, this is what it is. // // Note: The first rule has a value of false since // we don't want to mess with words that end with // double 's'. We normally wouldn't have to create // rules for words we don't want to mess with, but // the last rule (s) would catch double (ss) words // if we didn't stop before it got to that rule. $rules = array( 'ss' => false, 'os' => 'o', 'ies' => 'y', 'xes' => 'x', 'oes' => 'o', 'ies' => 'y', 'ves' => 'f', 's' => ''); // Loop through all the rules and do the replacement. foreach(array_keys($rules) as $key){ // If the end of the word doesn't match the key, // it's not a candidate for replacement. Move on // to the next plural ending. if(substr($word, (strlen($key) * -1)) != $key) continue; // If the value of the key is false, stop looping // and return the original version of the word. if($key === false) return $word; // We've made it this far, so we can do the // replacement. return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key]; } return $word; }
I can’t claim that this was my work, but here is the authors link where I got this from.
//use it like so echo depluralize('Products');// returns Product
https://sites.google.com/site/chrelad/notes-1/pluraltosingularwithphp
Happy days.