1 Replies - 698 Views - Last Post: 05 October 2011 - 03:38 PM Rate Topic: -----

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1509
  • View blog
  • Posts: 8,273
  • Joined: 07-September 06

Recursive regex pattern -- hopefully simple...

Posted 05 October 2011 - 03:27 PM

This is hopefully one of those things that I am only having trouble with because I am so close to the problem. It seems simple enough, I just can't make it work...

I am trying to extend my template engine (in PHP) to work with templates within templates.

This is what I mean:
[[function:date:Y-m-d:[[function:strtotime:-5 years]]]]


Which I am wanting to be evaluated to this:
date("Y-m-d", strtotime("-5 years"));


At the moment I have the code finding the first function correct, but it stops when it finds the first set or ]], and doesn't go on to the second set... That isn't explaining what I am attempting very well.

Another way:
I want it to first do:
[[function:strtotime:-5 years]]


and then do:
[[function:date:Y-m-d:<result>]]


Anyways, on to the code. I am currently using this regex to find the patterns:
/\[\[([^\]]*?)\]\]/


Here is the class (with things clipped):
class Replace{
  private static function _swap($matches){
    parts = explode(":", $matches[1]);
    if(count($parts) == 1){
      global $config;
      return $config[$parts[0]];
    }
    // ... - deals with specific cases, etc.
    return "";
  }
  public static function on($in){
    while(preg_match("/\[\[([^\]]*?)\]\]/", $in)){
      $in = preg_replace_callback("/\[\[([^\]]*?)\]\]/", "self::_swap", $in);
    }
    return $in;
  }
}


Any thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: Recursive regex pattern -- hopefully simple...

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1509
  • View blog
  • Posts: 8,273
  • Joined: 07-September 06

Re: Recursive regex pattern -- hopefully simple...

Posted 05 October 2011 - 03:38 PM

... and it was simple.

Basically, I just changed the pattern to:
/\[\[([^\[]*?)\]\]/


and it works (at least for the time being).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1