November 16, 2016 —John Koster
The 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:
finish($value, $cap)
1use Illuminate\Support\Str; 2 3// /home/path/ 4echo Str::finish('/home/path', '/'); 5 6// /home/path/ 7echo Str::finish('/home/path/', '/'); 8 9// Sentences should end with a period, or full stop.10echo Str::finish('Sentences should end with a period, or full stop', '.');11 12// 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;2 3// this_is_a_file_path.html4echo Str::finish(Str::slug('this is a file path','_'), '.html');
str_finish($value, $cap)
The str_finish
function is a shortcut to calling Str::finish
. This function is declared in the global namespace.
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.