The Blog

Laravel 5

Laravel 5 String Helpers: Replacing the Last Occurrence of a Value

John Koster
John Koster
December 6, 2017

The replaceLast helper method is used to replace the last occurrence of a given string with another string in a larger string. It is the opposite of the replaceFirst method. All three parameters must be strings and are required. An example use of this method includes replacing the last occurrence of a word in a sentence. In addition, there is a global str_replace_last helper function available which provides a shortcut for calling the replaceLast method.

Laravel 5

Laravel 5 String Helpers: ends_with global helper

John Koster
John Koster
December 6, 2017

The endsWith method in Laravel's Str class is used to check if a given string ends with any of the supplied characters. It returns true if the string ends with any of the characters, and false otherwise. You can use the endsWith method directly or the global ends_with helper function for convenience.

Laravel 5

Checking if a String Ends with a Particular Value in Laravel -Laravel 5.5 String Helper Method: endsWith

John Koster
John Koster
November 30, 2017

The endsWith method in Laravel's Str class is used to check if a string ends with any of the supplied characters. It returns true if the string ends with any of the characters and false otherwise. You can also use it to check if a string ends with any alphabetical character by combining it with PHP's range function. Additionally, there is a global function called ends_with that serves as a shortcut to calling Str::endsWith.

Laravel 5

Checking if a String Starts With a Particular Value in Laravel - Laravel 5.5 String Helper Method startsWith

John Koster
John Koster
November 30, 2017

The startsWith method in Laravel's Str class checks if a given string starts with any of the supplied needles. It works the opposite of the ends_with method, using the same rules. Laravel also provides a global starts_with helper function, which is a shortcut to calling Str::startsWith.

Laravel 5

Generating URLs from Strings in Laravel - Laravel 5.5 String Helper Method "slug"

John Koster
John Koster
November 30, 2017

The slug method in Laravel's Str class allows you to format a given title into a URI slug. It will convert the title to ASCII characters, replace whitespace with a separator, and reduce multiple separators to a single one. It will also trim any leading or trailing whitespace or separators. The slug method has a default separator of "-", but you can customize it as well as specify the language. Additionally, Laravel provides a global str_slug helper function as a shortcut to calling Str::slug.

Laravel 5

Laravel 5.5 String Helper Method: contains

John Koster
John Koster
November 30, 2017

The contains method in Laravel's Illuminate\Support\Str class allows you to check if any given elements exist within a string. By passing in a string and an array of elements, the method will return true if any of the elements are found in the string, and false otherwise. The method takes into account the case of both the string and the elements. Additionally, Laravel provides a global str_contains helper function that serves as a shortcut to calling Str::contains.

Laravel 5

Laravel 5.5 String Helper Method: finish

John Koster
John Koster
November 30, 2017

The finish helper method ensures that a given value always ends with a provided character or string. This is especially useful when constructing URIs or file paths. The method can be combined with other string manipulation methods, like slug, to generate interesting results. Additionally, there is a global str_finish helper function that provides a shortcut to calling Str::finish.

Laravel 5

Laravel 5.5 String Helper Method: kebab

John Koster
John Koster
November 30, 2017

The snake helper method is used to replace uppercase letters in a string with their lowercase variant, preceded by a - character. However, if the first character of the string is capitalized, it will be replaced without the - character. The kebab method removes excess whitespace from a string that is being converted. Both methods can be used interchangeably when using the - delimiter. Additionally, the kebab_case function serves as a shortcut for calling the Str::kebab method.

Laravel 5

Laravel 5.5 String Helper Method: snake

John Koster
John Koster
November 30, 2017

The snake helper method in Laravel replaces all uppercase letters within a string with the lowercased variant, while prefixing them with a given delimiter. However, if the first character of the string is already capitalized, it will be replaced by the corresponding lowercased variant without the delimiter. The method also trims any extra whitespace from the string being converted. Additionally, there is a global snake_case helper function available, which is a shortcut to calling Str::snake.

Laravel 5

Laravel 5.5 String Helper Method: title

John Koster
John Koster
November 30, 2017

This method allows you to convert a given value into a traditional print title format. It converts the value to lowercase and capitalizes the first character of each word. Note that it may change the case of every character in the string. In addition to the title method, you can also use the global title_case helper function as a shortcut for calling Str::title.

Latest posts

Where Things get Good: Moving on to Forte Development Phase 1

Wrapping up Forte Phase 0 with the lexer and parser now in private alpha, and kicking off Phase 1 wi...

Read more
Forte Update: Backtracking, Metadata, HTML Validation, and More

A Forte development update: the parser now supports backtracking, improvements to node metadata, ide...

Read more
Parsing HTML and Blade Attributes in Forte

Wrapping up attribute parsing in Forte's HTML parser, from simple HTML attributes to complex, edge-c...

Read more
Switch Statements and Parser Extensions in Forte

Exploring how Forte's parser extensions can be used to handle complex Blade directives like nested s...

Read more
Parsing Blade Comments in Forte

Digging into parsing Blade and HTML comments while building Forte's HTML parser for Laravel Blade.

Read more
Thoughts on HTML Elements and Blade Components in Forte

This week I’m tackling Forte's HTML parser - consolidating Blade, Flux, and Livewire components into...

Read more