Matching innermost braces with regex or strpos?

Posted by rich97 on Stack Overflow See other posts from Stack Overflow or by rich97
Published on 2010-03-16T15:20:04Z Indexed on 2010/03/16 15:21 UTC
Read the original article Hit count: 242

Filed under:
|
|

I have a sort of mini parsing syntax I made up to help me streamline my view code in cakephp. Basically I have created a table helper which, when given a dataset and (optionally) a set of options for how to format the data will render out a table, as opposed to me looping though the data and editing it manually.

It allows the user to be as complex or as simple as they like, it can get pretty powerful. However, In order to achieve this I had to make a simple parsing syntax. As a quick example the user would do something like so:

$this->Table->data = $userData;
$this->Table->elements['td']['data'] = array(
    '{:User.username:}',
    '{:User.created:}' => array('Time::nice')
);
echo $this->Table->render();

And when rendering the table would then generate:

<table>
    <tbody>
        <tr><td>rich97</td><td>Sun 21st 02:30pm</td></tr>
    </tbody>
</table>

The problem occurs then I try to nest the braces like so:

{:User.levels.iconClasses.{:User.access:}:}

Is there anyway I can only get the inner most brackets on the first time round and loop though until there are no matches? Or even do it in one go? Or even better use strpos?

Here is my regex as it stands:

 '/\{\:([^}]+)\:\}/'

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about regex