Complete guide to String and text manipulation in laravel

The string manipulation functions are found in the Illuminate\Support namespace and are callable on the Str object.

Most of the functions also have shorter snake_case aliases. For example, the Str::endsWith() method is identical to the global ends_with() function. We are free to use whichever one we prefer in our application.

Boolean functions

The following functions return the true or false values:

  • The is method checks whether a value matches a pattern. The asterisk can be used as a wildcard character as shown here:
    Str::is('projects/*', 'projects/php/'); // Returns true
  • The contains method, as shown in the following code, checks whether a string contains a given substring:
    Str::contains('Getting Started With Laravel', 'Python');
    // returns false
  • The startsWith and endsWith methods, as shown in the following code, check whether a string starts or ends with one or more substrings:
    Str::startsWith('.gitignore', '.git'); // Returns true
    Str::endsWith('index.php', ['html', 'php']); // Returns true

As you can see from the preceding examples, these methods are handy for validation filenames and similar dat

 

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares