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.
The easiest and most maintainable way to do this would be to just get the whole
plugin:class:methodstring with a simple/plugin:\S+/expression, thenexplode(':', $string).So, instead of the code above, you’d have something like: