Home  >  Article  >  PHP Framework  >  About Laravel7's convenient string operations

About Laravel7's convenient string operations

藏色散人
藏色散人forward
2021-04-12 16:46:232642browse

The following tutorial column will introduce the convenient string operations of Laravel7 to you in the laravel tutorial column. I hope it will be helpful to friends in need!

Friends who have used Laravel know that they have an understanding of Laravel’s built-in string processing function, the IlluminateSupportStr class.

Laravel 7 now provides a more object-oriented and fluent string manipulation library based on these functions. You can use String::of to create an IlluminateSupportStringable object, and then process the string in a chained operation based on the methods provided by the object:

Here we take a look at the official example first:

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

About Laravel7s convenient string operations

The above code comes from the official website release notes. We can easily see what each method does.

The first step is to wrap the string Laravel Framework 6.x using the Str::of() method. After that, we can use the various smooth operation methods provided by Laravel 7.

For example, trim() removes leading and trailing spaces. replace() replaces, slug() changes the string into the form of slug

The actual running result of the above code is:

laravel-framework-7x

About Laravel7s convenient string operations

Is the above method very simple to use?

Next we introduce several commonly used methods.

before() method

Returns everything before the given value in the string:
Str::of('my website is www.wjcms.net')->before('is');
// 'my website '

Similarly, if there is before(), there will be after()

after() method

Returns everything after the given value in the string. If the value does not exist in the string, the entire string will be returned:
Str::of('my website is www.wjcms.net')->after('is');
// ' www.wjcms.net'

append() method

Appends the given value to the string:
Str::of('vue')->append(' cli');
 
// 'vue cli'

lower () Method

Converts the string to lower case:
Str::of('LARAVEL FRAMEWORK')->lower();
 
// 'laravel framework'

upper() Method

Converts the given string to upper case:
Str::of('laravel')->upper();
 
// LARAVEL

title() method

Convert the given string to "first letter capitalization":
Str::of('my website is www.wjcms.net')->title();
 
// 'My Website Is Www.wjcms.net'

substr() method

Specify the given start and length parameters The string part: (Start from which character to intercept, how many digits to intercept)
Str::of('Laravel Framework')->substr(8);
 
// 'Framework'
Str::of('Laravel Framework')->substr(8, 6);
 
// 'Framew'

ucfirst() method

Capitalize the first letter of 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 the 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 introduces some commonly used methods. In fact, they are all operations in the document. I just can’t demonstrate the operation. For more usage, please check the document directly.

The following is a collection of all methods
About Laravel7s convenient string operations

##Related recommendations: The latest five Laravel video tutorials

The above is the detailed content of About Laravel7's convenient string operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete