Getting multiple texts between brackets using preg_match_all

Issue

I have to extract from string ALL texts which are between brackets.
So, for this string:

{s_company_name}–{s_nip_number}

I should receive s_company_name AND s_nip_number as a result.

Following this question: Capturing text between square brackets in PHP
I have created the following regular expression:

preg_match_all("/\{([^\]]*)\}/", $field['content'], $matches);

But for above example, it returns me only one result: "s_company_name}–{s_nip_number".

What am I doing wrong?

Solution

Use a lazy dot in your regex pattern:

preg_match_all("/\{(.*?)\}/", $field['content'], $matches);

Answered By – Tim Biegeleisen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published