Skip to content

Instantly share code, notes, and snippets.

@jeffhappens
Last active January 13, 2022 03:44
Show Gist options
  • Save jeffhappens/1a892c350c2ccda2e822 to your computer and use it in GitHub Desktop.
Save jeffhappens/1a892c350c2ccda2e822 to your computer and use it in GitHub Desktop.
Laravel Helper function that converts a string to a possessive
<?php
// Add this function to app/helpers.php
if ( ! function_exists('str_possessive')) {
/**
* Make a string possessive.
* @param string $string
* @return string
*/
function str_possessive($string) {
return $string.'\''.($string[strlen($string) - 1] != 's' ? 's' : '');
}
}
?>
In composer.json add this block to "autoload"
"files": [
"app/helpers.php"
]
// Run composer dump-autoload
// Usage:
<?php echo str_possessive('Your String'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment