Published on November 16, 2016
finish helper method will make sure that a given
$value always ends with exactly one occurrence of the
$cap. This helper method is incredibly useful when construction URIs or file paths. The
$cap can be any string of characters, and does not have to be only a single character.
The signature for the
finish helper method is:
1use Illuminate\Support\Str;23// /home/path/4echo Str::finish('/home/path', '/');56// /home/path/7echo Str::finish('/home/path/', '/');89// Sentences should end with a period, or full stop.10echo Str::finish('Sentences should end with a period, or full stop', '.');1112// Sentences should end with a period, or full stop.13echo Str::finish('Sentences should end with a period, or full stop.', '.');
Combine this method with
slug to create interesting results:
1use Illuminate\Support\Str;23// this_is_a_file_path.html4echo Str::finish(Str::slug('this is a file path','_'), '.html');
str_finish function is a shortcut to calling
Str::finish. This function is declared in the global namespace.
Published on July 28, 2014
Published on April 15, 2018