Skip to content

Instantly share code, notes, and snippets.

@PatricNox
Created August 9, 2019 09:11
Show Gist options
  • Save PatricNox/4805b84e4d83552ab48c9ddf97beadbc to your computer and use it in GitHub Desktop.
Save PatricNox/4805b84e4d83552ab48c9ddf97beadbc to your computer and use it in GitHub Desktop.
PHP function that removes the last part of a string.
/**
* Remove the last part of a string.
*
* This function removes the last part of a string, conditioned on spaces
* inbetween the parts. Like words, in a sentence.
*
* @param String $string The string.
**/
function str_remove_lastpart($string) {
$string_parts = explode(' ',$string);
unset($string_parts[count($string_parts)-1]);
return implode(' ',$string_parts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment