Last active
October 15, 2023 03:57
-
-
Save md-riaz/6f055dfdbf6c418300721837576e39a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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