我有一种迷你解析语法,我帮助我简化我的视图代码在cakephp。基本上我已经创建了一个表辅助程序,当给定一个数据集和(可选)一组选项,如何格式化数据将渲染一个表,而不是我循环通过数据和手动编辑它。
它允许用户像他们喜欢的一样复杂或简单,它可以变得非常强大。然而,为了实现这一点,我不得不做一个简单的解析语法。作为一个快速示例,用户可以这样做:
$ this-> Table-> data = $ userData; $ this-> Table-> elements ['td'] ['data'] = array('{:User.username:}',' created:}'=> array('Time :: nice')); echo $ this-> Table-> render();然后当渲染表时会生成:
< table> < tbody> < tr>< td> rich97< / td>< td> Sun 21st 02:30 pm </td> ;</tr> < / tbody> < / table>出现问题时,我尝试嵌套大括号:
{:User.levels.iconClasses。{:User.access:}:}有没有反正我只能得到最内层的括号在第一次循环和循环,直到没有匹配?或者甚至一次去做?
$ b这是我的正则表达式:
'/ \ {\:([^}] +)\:\} /'解决方案
'/ \ {:([^ {}] +):\} /'
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:
'/\{\:([^}]+)\:\}/'解决方案
Just add the opening brace to your negated character class:
'/\{:([^{}]+):\}/'
更多推荐
匹配最里面的大括号与正则表达式或strpos?
发布评论