0

I have a string like this.

[system]a[/system][system]b[/system][system]c[/system]

I want to return values between [system] and [/system] tags.

So for this example it should return a, b, c. (with preg_match_all function in php)

I tried to use this pattern \[system\](.*)\[\/system\] but it returns a[/system][system]b[/system][system]c.

It simply takes the first opening tag and finishes with last closing tag.

How can i do this?

kenarsuleyman
  • 920
  • 1
  • 6
  • 26

1 Answers1

1

Make your quantifier non-greedy (reluctant). Add a ? like this:

(.*?)

This will instruct the regex to match as few as possible instead of being greedy and matching as many as possible.

degant
  • 4,861
  • 1
  • 17
  • 29