阅读(2922)
赞(5)
Laravel 8 replaceMatches {#collection-method}
2021-07-05 09:43:52 更新
replaceMatches
方法用指定字符串替换字符串中使用指定正则表达式匹配到的所有部分:
use IlluminateSupportStr;
$replaced = Str::of('(+1) 501-555-1000')->replaceMatches('/[^A-Za-z0-9]++/', '')
// '15015551000'
replaceMatches
方法还接受一个闭包函数,它可作用于字符串中匹配到的每一部分,允许您在闭包函数中处理替换逻辑并返回替换值:
use IlluminateSupportStr;
$replaced = Str::of('123')->replaceMatches('/d/', function ($match) {
return '['.$match[0].']';
});
// '[1][2][3]'