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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.