November 30, 2017 —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 of the finish
method is:
1public static function finish(2 $value,3 $cap4 );
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(11 'Sentences should end with a period, or full stop',12 '.'13 );14 15// Sentences should end with a period, or full stop.16echo Str::finish(17 'Sentences should end with a period, or full stop.',18 '.'19 );
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
Helper FunctionThe 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.