Laravel 5.5 String Helper Method: finish

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.

#Signature

The signature of the finish method is:

1public static function finish(
2 $value,
3 $cap
4 );

#Example Use

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.html
4echo Str::finish(Str::slug('this is a file path','_'), '.html');

#Global str_finish Helper Function

The str_finish function is a shortcut to calling Str::finish. This function is declared in the global namespace.

Some absolutely amazing
people

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.