Search code examples
phppreg-match

PHP preg_match only one match


I have had some problems using preg_match, Instead of selecting 1 thing. It finds from the start of the first, to the end of the last.

preg_match('/@section\(\'(.*)\'\)(.*)@endsection/s', $content,$results

$Content :

@section('title')

Number 1

@endsection

@section('content')

Number 2

@endsection

But when i use the preg_match, the result comes out like :

Number 1

@endsection

@section('content')

Number 2

Solution

  • You are looking for non-greedy quantifier over here like as

    /@section\(\'(.*?)\'\)(.*?)@endsection/s
                  //^^      ^^
    

    Regex