Reputation: 23
I'm having a tough time sorthing this out. I'm working with a custom plugin system that presents things like this:
<plugin:class:method var1='hello', var2='yes' />
But, ':method' and the variables do not have to exist - only the 'class' is required. For instace, this should still be ok:
<plugin:class />
The problem I'm having is how to I get the regex to conditionally return things when the method and/or variables do not exist. So far, I can get results when all the pieces exist, but not otherwise - this is where I'm at (struggling on the first conditional):
$f = "/<plugin:(?P<class>\w+)(?(?=^:)?P<method>\w+)\s+(.*)\/>/sUi";
Things are working very well with the following code, it's simply a matter of being able to return all the pieces with the conditionals:
preg_replace_callback($f, array($this, 'processing'), $text);
Hope this makes some sense - and is even possible. Thanks.
Upvotes: 2
Views: 190
Reputation: 63442
The easiest and most maintainable way to do this would be to just get the whole plugin:class:method
string with a simple /plugin:\S+/
expression, then explode(':', $string)
.
So, instead of the code above, you'd have something like:
$f = "/<plugin:(\S+)\s+(.*?)\/>/sUi";
if (preg_match($f, $string, $matches)) {
$parts = explode($matches[1]);
if (!in_array('method', $parts))
{
// do whatever needs done if "method" is not present
}
// ...
}
Upvotes: 1