The Blog

Laravel

Laravel

Laravel String Helper Function: length

John Koster
John Koster
November 16, 2016

The length helper method in Laravel returns the length of a given value in PHP. It utilizes PHP's internal character encoding to calculate the length. For example, Str::length('12345') would return 5, and Str::length(str_repeat('n', 120)) would return 120. Additionally, str_repeat is a PHP function that repeats a given input a specified number of times, such as str_repeat('n', 120) resulting in a string with 120 occurrences of the character 'n'.

Laravel

Laravel String Helper Function: limit

John Koster
John Koster
November 16, 2016

The limit method is a useful helper function in Laravel's Str class that ensures a provided value does not exceed a specified limit. It appends a given end sequence if the value is trimmed. This is particularly useful when displaying lengthy user-generated content to avoid issues with UI layouts. The str_limit function is a shortcut to calling Str::limit and is defined in the global namespace.

Laravel

Laravel String Helper Function: lower

The lower helper method converts a given value to lowercase. Use the lower function by passing the desired value as an argument. For example, Str::lower('ALL LOWER CASED') will output "all lower cased".

Laravel

Laravel String Helper Function: parse_callback

John Koster
John Koster
November 16, 2016

Learn how to use the parseCallback helper method in Laravel to split a string with a class name and method name. With the $default parameter, you can specify a default method if no method name is provided. The parseCallback function returns an array containing the class name and method name. Check out some examples of usage to understand how it works.

Laravel

Laravel String Helper Function: quick_random

John Koster
John Koster
November 16, 2016

Learn how to use the quickRandom helper method in Laravel to generate random strings quickly. This method is similar to random, but does not rely on the OpenSSL extension. It generates strings from a pool of alpha-numeric characters. Keep in mind that quickRandom is not suitable for generating cryptographically secure strings. See examples of quickRandom output in the code snippet provided.

Laravel

Laravel String Helper Function: random

John Koster
John Koster
November 16, 2016

The random helper method generates a random string of the specified length and requires the OpenSSL extension to be installed and configured. If the OpenSSL extension is not installed, a RuntimeException will be thrown. The random method removes certain characters from the generated string. Additionally, there is a shortcut function called str_random that calls Str::random.

Laravel

Laravel String Helper Function: randomBytes

John Koster
John Koster
November 16, 2016

The randomBytes helper method generates a string of random bytes with the specified length. It is recommended over the random method as it produces more random results. The helper method requires the OpenSSL extension for PHP 5 and uses the openssl_random_pseudo_bytes function. In PHP 7, the random_bytes function is utilized instead. Laravel version 5.2 deprecated the randomBytes helper method in favor of PHP's random_bytes function. If OpenSSL is not installed or configured, or if the method fails to generate a random string, a RuntimeException will be thrown.

Laravel

Laravel String Helper Function: singular

John Koster
John Koster
November 16, 2016

The singular helper method in Laravel is used to convert a plural word into its singular form. This method is the opposite of the plural method. It follows similar internal mechanisms as the plural method. In Laravel, you can also use the str_singular() function as a shortcut to calling Str::singular.

Laravel

Laravel String Helper Function: slug

John Koster
John Koster
November 16, 2016

The slug helper method in Laravel is used to format a given title into a URI slug. It converts the title to ASCII, replaces whitespace and separators with a specified character, and trims whitespace and separators from the beginning and end of the string. The str_slug function serves as a shortcut to the Str::slug method, making it easier to use in global namespace.

Laravel

Laravel String Helper Function: snake

John Koster
John Koster
November 16, 2016

The snake helper method in Laravel's Str class converts a string to snake case by replacing uppercase letters with the lowercased variant, optionally separated by a delimiter. The method also trims whitespace from the string being converted. Laravel provides a global snake_case function as a shortcut to calling Str::snake.

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