Laravel 8 matchAll {#collection-method}

2021-07-19 10:33 更新

matchAll 方法將會(huì)返回一個(gè)集合,該集合包含了指定字符串中與指定正則表達(dá)式匹配的部分:

use Illuminate\Support\Str;

$result = Str::of('bar foo bar')->matchAll('/bar/');

// collect(['bar', 'bar']) 

如果您在正則表達(dá)式中指定了一個(gè)匹配組, Laravel 將會(huì)返回與該組匹配的集合:

use Illuminate\Support\Str;

$result = Str::of('bar fun bar fly')->matchAll('/f(\w*)/');

// collect(['un', 'ly']); 

如果沒有找到任何匹配項(xiàng),則返回空集合。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)