辅助函数 流式字符串函数
Laravel 辅助函数 流式字符串
流式字符串函数为处理字符串值提供了更加平滑的、面向对象的接口,通过这些函数,我们可以将多个字符串操作以方法链的形式进行流式处理,这种方式比传统的字符串处理从语法角度来看可读性更好。
after()
after
方法返回字符串中给定值之后的所有字符,如果传入值在字符串中不存在的话则返回整个字符串:
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->after('This is');
// ' my name'
afterLast()
afterLast
方法会返回字符串中给定值最后一次出现位置之后的所有字符,如果该值在字符串中不存在则返回整个字符串:
use Illuminate\Support\Str;
$slice = Str::of('App\Http\Controllers\Controller')->afterLast('\\');
// 'Controller'
append()
append
方法用于追加给定值到字符串:
use Illuminate\Support\Str;
$string = Str::of('Taylor')->append(' Otwell');
// 'Taylor Otwell'
basename()
basename
方法会返回给定字符串最后一个/位置之后的尾部:
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->basename();
// 'baz'
如果需要,你还可以提供需要从尾部移除的「扩展名」:
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
// 'baz'
before()
before
方法会返回字符串中给定值位置之前的所有字符:
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->before('my name');
// 'This is '
beforeLast()
beforeLast
方法会返回字符串中给定值最后一次出现位置之前的所有字符:
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->beforeLast('is');
// 'This '
camel()
camel
方法将给定字符串转化为形如 camelCase 的驼峰风格:
use Illuminate\Support\Str;
$converted = Str::of('foo_bar')->camel();
// fooBar
contains()
contains
方法判断给定字符串是否包含给定值(大小写敏感):
use Illuminate\Support\Str;
$contains = Str::of('This is my name')->contains('my');
// true
还可以传递数组值判断给定字符串是否包含数组中的任意值:
use Illuminate\Support\Str;
$contains = Str::of('This is my name')->contains(['my', 'foo']);
// true
containsAll()
containsAll
方法用于判断给定字符串是否包含所有数组值:
use Illuminate\Support\Str;
$containsAll = Str::of('This is my name')->containsAll(['my', 'name']);
// true
dirname()
diranme
方法会返回给定字符串的父级部分(一般用于路径或目录字符串):
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->dirname();
// '/foo/bar'
此外 ,你还可以指定目录层级:
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->dirname(2);
// '/foo'
endsWith()
endsWith
方法用于判断字符串是否以给定值结尾:
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith('name');
// true
你还可以传递数组来判断给定字符串是否以数组中的任意值作为结尾:
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith(['name', 'foo']);
// true
$result = Str::of('This is my name')->endsWith(['this', 'foo']);
// false
exactly()
exactly
方法会判断指定字符串是否和另一个字符串匹配:
use Illuminate\Support\Str;
$result = Str::of('Laravel')->exactly('Laravel');
// true
explode()
explode
方法会通过给定分割符对字符串进行切分,返回一个包含字符串分割后各个部分的集合:
use Illuminate\Support\Str;
$collection = Str::of('foo bar baz')->explode(' ');
// collect(['foo', 'bar', 'baz'])
finish()
finish
方法添加给定值的单例到字符串结尾 —— 如果原字符串不以给定值结尾的话:
use Illuminate\Support\Str;
$adjusted = Str::of('this/string')->finish('/');
// this/string/
$adjusted = Str::of('this/string/')->finish('/');
// this/string/
is()
is
方法判断给定字符串是否与给定模式匹配,星号可用于表示通配符:
use Illuminate\Support\Str;
$matches = Str::of('foobar')->is('foo*');
// true
$matches = Str::of('foobar')->is('baz*');
// false
isAscii()
isAscii
方法会判断给定字符串是否是 ASCII 字符串:
use Illuminate\Support\Str;
$result = Str::of('Taylor')->isAscii();
// true
$result = Str::of('ü')->isAcii();
// false
isEmpty()
isEmpty
方法会判断给定字符串是否为空:
use Illuminate\Support\Str;
$result = Str::of(' ')->trim()->isEmpty();
// true
$result = Str::of('Laravel')->trim()->isEmpty();
// false
kebab()
kebeb
方法将给定字符串转化为形如 kebab-case 风格(短划线连接)的字符串:
use Illuminate\Support\Str;
$converted = Str::of('fooBar')->kebab();
// foo-bar
length()
length
方法会返回给定字符串的长度:
use Illuminate\Support\Str;
$length = Str::of('Laravel')->length();
// 7
limit()
limit
方法以指定长度截断字符串:
use Illuminate\Support\Str;
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
// The quick brown fox...
还可以传递第三个参数来改变字符串末尾字符:
use Illuminate\Support\Str;
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20, ' (...)');
// The quick brown fox (...)
lower
lower
方法将给定字符串转化为小写格式:
use Illuminate\Support\Str;
$result = Str::of('LARAVEL')->lower();
// 'laravel'
match()
match
方法将返回与给定正则表达式模式匹配的子字符串:
use Illuminate\Support\Str;
$result = Str::of('foo bar')->match('/bar/');
// 'bar'
$result = Str::of('foo bar')->match('/foo (.*)/');
// 'bar'
matchAll()
matchAll
方法将返回与给定正则表达式模式匹配的子字符串集合:
use Illuminate\Support\Str;
$result = Str::of('bar foo bar')->matchAll('/bar/');
// collect(['bar', 'bar'])
如果你在正则表达式中指定了匹配分组,Laravel 会返回那个分组匹配的集合:
use Illuminate\Support\Str;
$result = Str::of('bar fun bar fly')->match('/f(\w*)/');
// collect(['un', 'ly']);
如果没有匹配到任何结果,将返回一个空集合
plural()
plural
方法会将单个单词字符串转化为复数格式,该方法目前仅支持英语:
use Illuminate\Support\Str;
$plural = Str::of('car')->plural();
// cars
$plural = Str::of('child')->plural();
// children
你可以提供一个整型数字作为第二个参数传递到该方法来获取字符串的单数和复数格式:
use Illuminate\Support\Str;
$plural = Str::of('child')->plural(2);
// children
$plural = Str::of('child')->plural(1);
// child
prepend()
prepend
方法可以在字符串前面添加值:
use Illuminate\Support\Str;
$string = Str::of('Framework')->prepend('Laravel ');
// Laravel Framework
replace()
replace
方法可以替换字符串中的给定字串:
use Illuminate\Support\Str;
$replaced = Str::of('Laravel 6.x')->replace('6.x', '7.x');
// Laravel 7.x
replaceArray
replaceArray
方法使用数组在字符串序列中替换给定值:
use Illuminate\Support\Str;
$string = 'The event will take place between ? and ?';
$replaced = Str::of($string)->replaceArray('?', ['8:30', '9:00']);
// The event will take place between 8:30 and 9:00
replaceFirst()
replaceFirst
方法会在字符串中给定值第一次出现的位置进行替换:
use Illuminate\Support\Str;
$replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a');
// a quick brown fox jumps over the lazy dog
replaceLast()
replaceLast
方法会在字符串中给定值最后一次出现的位置进行替换:
use Illuminate\Support\Str;
$replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a');
// the quick brown fox jumps over a lazy dog
replaceMatches()
replaceMatches
方法会通过给定替换字符串替换原字符串中所有匹配给定模式的部分:
use Illuminate\Support\Str;
$replaced = Str::of('(+1) 501-555-1000')->replaceMatches('/[^A-Za-z0-9]++/', '')
// '15015551000'
replaceMatches 方法还可以接收一个闭包,该闭包会在每次字符串匹配给定部分时调用,从而可以将替换逻辑放到闭包中实现,最后返回替换后的值:
use Illuminate\Support\Str;
$replaced = Str::of('123')->replaceMatches('/\d/', function ($match) {
return '['.$match[0].']';
});
// '[1][2][3]'
start()
start
方法会在字符串不是以给定值开头的情况下,添加给定值到字符串:
use Illuminate\Support\Str;
$adjusted = Str::of('this/string')->start('/');
// /this/string
$adjusted = Str::of('/this/string')->start('/');
// /this/string
upper()
upper
方法会将给定字符串转化为大写格式:
use Illuminate\Support\Str;
$adjusted = Str::of('laravel')->upper();
// LARAVEL
title()
title
方法会将给定字符串转化为形如 Title 的这种风格:
use Illuminate\Support\Str;
$converted = Str::of('a nice title uses the correct case')->title();
// A Nice Title Uses The Correct Case
singular()
singular
方法将字符串转化为单数形式,该方法目前只支持英文:
use Illuminate\Support\Str;
$singular = Str::of('cars')->singular();
// car
$singular = Str::of('children')->singular();
// child
slug()
slug
方法将给定字符串生成 URL 友好的格式:
use Illuminate\Support\Str;
$slug = Str::of('Laravel Framework')->slug('-');
// laravel-framework
snake()
snake
方法将给定字符串转换为形如 snake_case 格式(下划线连接风格)字符串:
use Illuminate\Support\Str;
$converted = Str::of('fooBar')->snake();
// foo_bar
startsWith()
startsWith
方法用于判断传入字符串是否以给定值开头:
use Illuminate\Support\Str;
$result = Str::of('This is my name')->startsWith('This');
// true
studly()
studly
方法将给定字符串转化为形如 StudlyCase 的、单词开头字母大写的格式:
use Illuminate\Support\Str;
$converted = Str::of('foo_bar')->studly();
// FooBar
substr()
substr
方法通过指定开头字符和长度截取字符串并返回:
use Illuminate\Support\Str;
$string = Str::of('Laravel Framework')->substr(8);
// Framework
$string = Str::of('Laravel Framework')->substr(8, 5);
// Frame
trim()
trim
方法会清除给定字符串前后的空格:
use Illuminate\Support\Str;
$string = Str::of(' Laravel ')->trim();
// 'Laravel'
$string = Str::of('/Laravel/')->trim('/');
// 'Laravel'
ucfirst()
ucfirst
方法会以首字母大写格式返回给定字符串:
use Illuminate\Support\Str;
$string = Str::of('foo bar')->ucfirst();
// Foo bar
whenEmpty()
whenEmpty
方法会在字符串为空时调用给定闭包,如果闭包有返回值,则该返回值被 whenEmpty 方法返回,否则,字符串实例本身会被返回:
use Illuminate\Support\Str;
$string = Str::of(' ')->whenEmpty(function ($string) {
return $string->trim()->prepend('Laravel');
});
// 'Laravel'
words()
words
方法会限制字符串单词个数:
use Illuminate\Support\Str;
$string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
// Perfectly balanced, as >>>