Laravel 8 matchAll {#collection-method}

2021-07-19 10:33 更新

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')->matchAll('/f(\w*)/');

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

如果沒有找到任何匹配項,則返回空集合。

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號