The Blog

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.

Laravel

Laravel String Helper Function: starts_with

John Koster
John Koster
November 16, 2016

startsWith is a helper method in Laravel that checks if a given string starts with any of the specified values. It is the opposite of the ends_with method and follows the same rules of use. The starts_with function provides a shortcut to calling Str::startsWith and is defined in the global namespace. You can use this method to easily check if a string begins with a specific substring.

Laravel

Laravel String Helper Function: str_plural

John Koster
John Koster
November 16, 2016

The plural helper method in Laravel is used to return the plural version of a given value. It applies general rules to strings and handles special cases. The str_plural function is a shortcut to calling Str::plural.

Laravel

Laravel String Helper Function: str_replace_first

John Koster
John Koster
November 16, 2016

Learn how to use the replaceFirst() helper method in PHP to replace the first occurrence of a string with another string in a given subject string. This method requires three parameters - the search string, the replacement string, and the subject string. Explore examples and a shortcut function called str_replace_first() that can be used to call Str::replaceFirst in your code.

Laravel

Laravel String Helper Function: str_replace_last

John Koster
John Koster
November 16, 2016

Learn how to use the replaceLast helper method in PHP to replace the last occurrence of a string with another string. The method requires three parameters: $search, $replace, and $subject, all of which must be strings. Additionally, there is a shortcut to calling the replaceLast method using the global str_replace_last function.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more