I've got the following that matches anything within curly braces automatically and it works great.

preg_match_all('/\{(.*?)\}/i', $this->tplfile, $matches );

I'm building a template system and I'm giving the option to have the system automatically parse text placeholders without you having to specify it like the knocked up example below.

example;

$sys->assign_vars(array(
'hello_user' =>$lang['hello_user'],
'the_time' => $lang['cuurent_time_is']
));

There are specific entries however, that are prefix with a single character and a following underscore.

{u_username}{s_site_title}

I'm been working on it for over an hour trying to accomplish it only matching within curly braces that are not prefixed with a single character and underscore.

My results are either
a) nothing is displayed
b) displays everything
c) removes the first letter from each word.

Can someone amend my code to get the desired affect please.

Thanks in advanced.

    Perhaps something like this then?

    Example:

    $str = '{u_username} blah {s_site_title} blah blah {some_title} blah blah {site_name}';
    preg_match_all('#{[a-z]{2,}[a-z_]+}#i', $str, $matches);
    echo '<pre>'.print_r($matches[0], true);
    

    Output:

    Array
    (
        [0] => {some_title}
        [1] => {site_name}
    )
    
      preg_match_all('/\{(?<!\d{1}_).*?\}/i', $test, $matches );
      preg_match_all('/\{(^[\d{1}_])(.*?)\}/i', $test, $matches );
      preg_match_all('/\{(?<!\d{1}_)[a-z_]\}/i', $test, $matches );

      I tried the above, none of which worked. Yours does though, can you explain it please if you don't mind.

        Sure, I'll explain. After the opening delimiter;

        [a-z]{2,} means match 2 or more letters...
        [a-z]+ If there is an underscore, the first part will stop and then check this part, which is match letters and or underscores, one or more times (due to the + quantifier). In this case, that part could also be written as [a-z] (= zero or more times).
        The 'i' modifier after the closing delimiter makes things case insensitive.. so [a-z] is treated as [a-zA-Z]

        You could read up more about regex in the following links:

        http://www.regular-expressions.info/
        http://weblogtoolscollection.com/regex/regex.php

        These should be more than enough to help get you started. Then, if you really want to get on the ball with regex, you check out the book that is hailed as the 'regex bible':

        http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239475391&sr=8-1

          I'm reading up on them sites you gave me.

          The code works but I think it would work better if it was possible to match anything that is not prefixed. All prefixes will be 1 letter followed by an underscore.

          The example you gave works well but if it was needed for a single 2 letter word like {or} or {it}, It misses them out.

            preg_match_all('/\{[sue]_{1}[a-z_]+\}/i', $test, $matches );

            I've done the above which will match things like;

            {s_site_something}
            {u_user_something}
            or
            {e_somehting}

            How do I make it not find these occurences and find anything not as the above.

              First off, you never need to specify {1}, since that is what's matched by default: one of the preceeding character(s).

              // match one letter, followed by anything other than _, followed by anything.
              '/[a-z][^_].*/i
              
              // or, match anything, followed by anything other than _, followed by anything
              '/.[^_].*/i

                Thanks I'm using the below;

                /\{[a-z][^_][a-z_]*\}/i
                  saint4;10928439 wrote:

                  I'm reading up on them sites you gave me.

                  The code works but I think it would work better if it was possible to match anything that is not prefixed. All prefixes will be 1 letter followed by an underscore.

                  The example you gave works well but if it was needed for a single 2 letter word like {or} or {it}, It misses them out.

                  To match stuff like {or} for example, simply change my pattern from:

                  #{[a-z]{2,}[a-z_]+}#i

                  to

                  #{[a-z]{2,}[a-z_]*}#i

                  Notice the change in quantifiers from + to . This way, it will match {, then 2 letters, but since the last part of [a-z_] has the star quantifier, it is zero or more times...so in essence, it becomes more-or-less optional.

                  Also note that you don't need to escape the { and } characters within the pattern.

                    Write a Reply...