开发者

Regular expression filtering - mimicking Expression Engine's template parsing

I'd like to mimick (reproduce) Expression Engine's fantastic template parsing method. (Pls don't ask me why not using it :))

While i'm able to find and parse simple tags like

{example_param = "param_value"}

i cannot parse tags where closing tag added:

{cyclic_param}
...
{/cyclic_param}

This is the pattern i'm using:

'/[\{^\/](.*)\}开发者_JAVA技巧/iU'

but it's returns {/cyclic_param} too.

I know there are zillions of regexp tutors out there but this is the thing i cannot understand ever :( (And i cannot figure out from EE's source)

How can i find opening and closing tags (with their inner blocks too) with PHP's regexp?

Thanks for your help!


 preg_match('~{(\w+)}(.+?){/\1}~s', $r, $m);

content will be in $m[2].

this won't handle nesting though.

/edit: complete example

    $text = "
    foo {single1=abc}
    bar {double1} one {/double1}
    foo {single2=def}
    bar {double2} two {/double2}


    ";

    $tag = '~
        {(\w+)}(.+?){/\1}
        |
        {(\w+)=(.+?)}
    ~six';

    preg_match_all($tag, $text, $m, PREG_SET_ORDER);
    foreach($m as $p) {
        if(isset($p[3]))
            echo "single $p[3] with param $p[4]\n";
        else
            echo "double $p[1] with content $p[2]\n";
    }


I think this is proper for You:

'/\{[^\/]*\}/iU'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜