bluebyyou Posted February 17, 2010 Share Posted February 17, 2010 <?php $str = "Some {sample|example|variable} text. More {text|things|stuff} text."; $pattern ="/\{.*\}/is"; preg_match_all($pattern, $str, $matches); foreach ($matches as $val) { print_r($val); } ?> It outputs: Array ( [0] => {sample|example|variable} text. More {text|things|stuff} ) ------------- What I want the output to be is each string between the {}'s to be parsed out individually, I just am having a hell of a time writing the proper regular expression. Can someone point me in the right direction? Link to comment https://forums.phpfreaks.com/topic/192404-preg_match_all-for-string-between-or-braces/ Share on other sites More sharing options...
cags Posted February 17, 2010 Share Posted February 17, 2010 It's because by default .* is a greedy search, meaning it will keep matching whilst it can. You could make the search lazy by adding a question mark after the .* match. Personally thought I'd opt to use [^}]* as your match instead. '#\{[^}]*\}#s' Link to comment https://forums.phpfreaks.com/topic/192404-preg_match_all-for-string-between-or-braces/#findComment-1013794 Share on other sites More sharing options...
bluebyyou Posted February 17, 2010 Author Share Posted February 17, 2010 Thanks, that did the trick! Link to comment https://forums.phpfreaks.com/topic/192404-preg_match_all-for-string-between-or-braces/#findComment-1013839 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.