PHP preg_match 只有一场比赛
PHP preg_match only one match
我在使用 preg_match 时遇到了一些问题,而不是选择 1 个东西。它从第一个开始查找到最后一个结束。
preg_match('/@section\(\'(.*)\'\)(.*)@endsection/s', $content,$results
$内容:
@section('title')
Number 1
@endsection
@section('content')
Number 2
@endsection
但是当我使用 preg_match 时,结果如下:
Number 1
@endsection
@section('content')
Number 2
您正在寻找非贪婪量词,例如 as
/@section\(\'(.*?)\'\)(.*?)@endsection/s
//^^ ^^
我在使用 preg_match 时遇到了一些问题,而不是选择 1 个东西。它从第一个开始查找到最后一个结束。
preg_match('/@section\(\'(.*)\'\)(.*)@endsection/s', $content,$results
$内容:
@section('title')
Number 1
@endsection
@section('content')
Number 2
@endsection
但是当我使用 preg_match 时,结果如下:
Number 1
@endsection
@section('content')
Number 2
您正在寻找非贪婪量词,例如 as
/@section\(\'(.*?)\'\)(.*?)@endsection/s
//^^ ^^