Fastest / most efficient way to grab everything between { and }?

I’m trying to create a function that accepts 1 string for an argument and then takes everything between curly braces throughout the string. So for example, if this supposed function I speak of accepts the following:


It would then create another variable (array?) with the following in it:

(I’m sure you get my point here.)

Anyway, I’m wondering if using substr() is the best to use for this? Or should I use some pattern expression? I’ve read multiple places that using a pattern is least efficient and most time consuming, but I wanted to run this by you guys to see what you all thought.

The function itself would have multiple lines of code (but not many going by the example I googled quickly earlier).

Once the function is set up to do what it needs to do then you could include it in either a php class file which handles “non standard” string functions or put it in a php library file after which it to would be a single function call whenever you want to use it.

In reference to performance issues, I doubt very much the difference in execution time would be noticable unless the string was extremely long.

could use explode

Your fine with preg_match_all()

Don’t make problems more complicated than they need to be unless no other alternative is present.

simple, stupid – the next person looking at your code with thank you

$matches = array();
$str = 'BLAH BLAH {my} BLAH BLAH {variables}!

BLAH BLAH {are} BLAH BLAH {within}!

BLAH BLAH {curly} BLAH BLAH {braces}!';


echo '<pre>',print_r($matches),'</pre>';

Let Google be your friend :slight_smile:

Here’s some example code on how do do it.

I’d go with a regexp here without any doubt.
Optimizing something on this level probably won’t affect performance of your application in any way.
Yes, you can achieve the same end result with str_ functions, however nothing guarantees that the code done with str_ functions will be any more readable / maintainable than using a simple regexp.
In terms of performance, you won’t lose much processing power (we’re talking about milliseconds here, not a single web user can claim they notice a difference between 0.001 and 0.002 seconds of execution time).

Me too, if only in terms of “developer efficiency”—it’s a quick one-liner to get the array of strings with preg_match_all. While certainly possible (and not-so-difficult) with basic string functions, it’s certainly not a single function call. Then again, if the OP has no idea what a quantifier or character class is then it may not be so “efficient”.

There may be sizable gains/losses in “performance”, though to make any hints in one direction or the other would be very much premature since we have no idea of the intended use of any approaches that we offer here nor the myriad of variables which come together to affect said performance.

A few questions for Wolf_22:

  • how familiar are you with regular expressions?
  • ditto for the various string functions?
  • what is prompting this desire to be “efficient” and “fast”?
  • is your BLAH example representative of the text that you will really be using?