Jump to content

preg_match_all easy question


ivobrabec2

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/169738-preg_match_all-easy-question/
Share on other sites

@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].

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.