Skip to content

Instantly share code, notes, and snippets.

@md-riaz
Last active October 15, 2023 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save md-riaz/6f055dfdbf6c418300721837576e39a6 to your computer and use it in GitHub Desktop.
Save md-riaz/6f055dfdbf6c418300721837576e39a6 to your computer and use it in GitHub Desktop.
<?php
function singular(string $word)
{
//"-es" is used for words that end in "-x", "-s", "-z", "-sh", "-ch" in which case you add
if (substr($word, -2) == "es") {
if (substr($word, -4) == "sses") { // eg. 'addresses' to 'address'
return substr($word, 0, -2);
} elseif (substr($word, -3) == "ses") { // eg. 'databases' to 'database' (necessary!)
return substr($word, 0, -1);
} elseif (substr($word, -3) == "ies") { // eg. 'countries' to 'country'
return substr($word, 0, -3) . "y";
} elseif (substr($word, -3, 1) == "x") {
return substr($word, 0, -2);
} elseif (substr($word, -3, 1) == "s") {
return substr($word, 0, -2);
} elseif (substr($word, -3, 1) == "z") {
return substr($word, 0, -2);
} elseif (substr($word, -4, 2) == "sh") {
return substr($word, 0, -2);
} elseif (substr($word, -4, 2) == "ch") {
return substr($word, 0, -2);
} else {
return rtrim($word, "s");
}
} else {
return rtrim($word, "s");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment