Home > PHP Framework > Laravel > body text

About Laravel7's convenient string operations

藏色散人
Release: 2021-04-13 08:57:51
forward
2643 people have browsed it

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();
Copy after login

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
Copy after login

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 '
Copy after login

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'
Copy after login

append() method

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

lower () Method

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

upper() Method

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

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'
Copy after login

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'
Copy after login
Str::of('Laravel Framework')->substr(8, 6);
 
// 'Framew'
Copy after login

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'
Copy after login

words() method

Limit the number of words in the string:
Str::of('my website is www.wjcms.net')->words(3, ' ...');
 
// 'my website is ...'
Copy after login

length() method

Return the length of the string:
Str::of('Laravel framework')->length();
 
// 17
Copy after login

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!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template