ivobrabec2 Posted August 11, 2009 Share Posted August 11, 2009 HI, i am new to the regex and i cannot figure out how to solve this: $source="<<##red##>><<##green##>><<##blue##>>"; $exp="/<<##(.*)##>>/"; preg_match_all($exp,$source,$result); i need the $result to be array with $result[0]="red";$result[1]="green";$result[2]="blue"; What is the correct $exp to make this result? Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/169738-preg_match_all-easy-question/ Share on other sites More sharing options...
JonnoTheDev Posted August 11, 2009 Share Posted August 11, 2009 <?php $source = "<<##red##>><<##green##>><<##blue##>>"; preg_match_all('/<<##([a-z]+)##>>/', $source, $result, PREG_PATTERN_ORDER); foreach($result[1] as $colour) { print $colour."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169738-preg_match_all-easy-question/#findComment-895495 Share on other sites More sharing options...
nrg_alpha Posted August 11, 2009 Share Posted August 11, 2009 @neil PREG_PATTERN_ORDER is the default, and as a result is not necessary to list that flag. @OP, if the contents will contain more than simply a-z between <<## and ##>>, you can change neil's goup capturing parenthesis to something like ([^#]+), otherwise neil's will work just fine (if there is the possibility of upper case characters, you can add the i modifier after the closing delimiter). In general, using .* (as you have done in your post) is frowned upon and is a bad idea. It certainly has its uses.. but on the whole, given the nature of how it operates, you'll more than likely receive incorrect results. You can read about this here (post #11 and #14) [it deals with .+, but the principal is the same]. Quote Link to comment https://forums.phpfreaks.com/topic/169738-preg_match_all-easy-question/#findComment-895731 Share on other sites More sharing options...
JonnoTheDev Posted August 11, 2009 Share Posted August 11, 2009 PREG_PATTERN_ORDER is the default, and as a result is not necessary to list that flag. force of habbit Quote Link to comment https://forums.phpfreaks.com/topic/169738-preg_match_all-easy-question/#findComment-895993 Share on other sites More sharing options...
.josh Posted August 11, 2009 Share Posted August 11, 2009 you forced a hobbit to do what....? Quote Link to comment https://forums.phpfreaks.com/topic/169738-preg_match_all-easy-question/#findComment-896030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.