Talking about the convenient string operation of laravel 7

Time:2021-1-14

Talking about the convenient string operation of laravel 7

Friends who have used laravel know that they know the built-in string processing function of laravel, illuminatesupportstr class.

Laravel 7 now provides a more object-oriented, smoother string manipulation library based on these functions. You can use string:: of to create an illuminatesupportstringable object, and then process the string in a chain operation based on the method provided by the object

Here we take a look at the official example:

return (string) Str::of('  Laravel Framework 6.x ')
                ->trim()
                ->replace('6.x', '7.x')
                ->slug();

Talking about the convenient string operation of laravel 7

The above code comes from the release notes on the official website. It’s easy to see what each method does.

The first step is to wrap the string laravel framework 6. X with the str:: of() method, and then we can use the various smooth operation methods provided by laravel 7.

For example, trim () removes the leading and trailing spaces. Replace (), slug () changes the string to slug

The actual running result of the above code is as follows:

laravel-framework-7x

Talking about the convenient string operation of laravel 7

Is the above method very simple to use?

Next, we introduce several common methods.

Before() method

Returns everything in a string before the given value:

Str::of('my website is www.wjcms.net')->before('is');
// 'my website '

Similarly, if you have before (), you will have after ()

After() method

Returns everything in a string after the given value. If the value does not exist in the string, the entire string is returned:

Str::of('my website is www.wjcms.net')->after('is');
// ' www.wjcms.net'

Append() method

Attach the given value to the string:

Str::of('vue')->append(' cli');
 
// 'vue cli'

Lower() method

Convert string to lowercase:

Str::of('LARAVEL FRAMEWORK')->lower();
 
// 'laravel framework'

Upper () method

Converts the given string to uppercase:

Str::of('laravel')->upper();
 
// LARAVEL

Title () method

Converts the given string to uppercase:

Str::of('my website is www.wjcms.net')->title();
 
// 'My Website Is Www.wjcms.net'

Substr() method

The part of the string specified by the given start and length parameters: (from which character to intercept, how many bits to intercept)

Str::of('Laravel Framework')->substr(8);
 
// 'Framework'
Str::of('Laravel Framework')->substr(8, 6);
 
// 'Framew'

Ucfirst() method

Capitalizes the given string:

Str::of('my website is www.wjcms.net')->ucfirst();
 
// 'My website is www.wjcms.net'

Words () method

Limit the number of words in a string:

Str::of('my website is www.wjcms.net')->words(3, ' ...');
 
// 'my website is ...'

Length() method

Return the length of the string:

Str::of('Laravel framework')->length();
 
// 17

OK, the above describes some common methods. In fact, they are all operations on the document. I just don’t want to show them again. For more usage, please check the document directly

Here is a collection of all the methods
Talking about the convenient string operation of laravel 7

Talking about the convenient string operation of laravel 7

Recommended Today

Introduction to vernacular spring cloud

First of all, let me show you a picture. If you don’t understand this picture very well, I hope you will suddenly realize it after reading my article. What is spring cloud Building a distributed system doesn’t need to be complex and error prone. Spring cloud provides a simple and easy to accept programming model […]