Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:27:09+00:00 2026-05-23T06:27:09+00:00

i use this pattern ‘/(\{(\w+)\}(.*))?\{%(\w+)%\}((.*)\{\/(\w+)\})?/i’ to extract tags from a template using preg_match function.

  • 0

i use this pattern '/(\{(\w+)\}(.*))?\{%(\w+)%\}((.*)\{\/(\w+)\})?/i' to extract tags from a template using preg_match function.

sample template:

<table id="middle" cellspacing="0px" cellpadding="0px">
    {middle}
    <tr>
        {left}<td>{%left%}</td>{/left}
        <td>{%middle%}{%content%}</td>
        {right}<td>{%right%}</td>{/right}
    </tr>
    {/middle}
</table>

how to make sure that start and end of each tag truly match it’s name
in this example middle tag matches for both middle and content while it should just match middle tag

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:27:09+00:00Added an answer on May 23, 2026 at 6:27 am

    I think the best way to solve this problem would be do this in a few different steps.

    First, you should use preg_replace_callback with /(?>{([^}]+)})(.*)?{\/\1}/sim as your regular expression. This will find the top level {tag}{/tag}. The $matches[2] will contain the content (without the tags) while $matches1 will contain the tag itself.

    You should create a function that’s called recursively so that in the callback, it’s called again on $matches[2] so find the children {tags} just in case there are more. This is how you’ll go through the tree.

    Finally, you should make a third function that handles the {%tag%}. I would use preg_replace_callback again and make use of the switch statement to handle the tag name.

    This should point you in the right direction.

    EDIT: Here is a fully functional demo of what I described above:\

    <?php
    
    $content = <<<END
    {a}
      {b}I like {%first%} {%last%} a {c}lot{/c}.{/b}
    {/a}
    END;
    
    echo find_tags($content);
    
    function find_tags($content)
    {
      return preg_replace_callback('/(?>{([^}]+)})(.*)?{\/\1}/sim', 'find_tags_callback', $content);
    }
    
    function find_tags_callback($matches)
    {
      // Find and process any children tag pairs.
      $matches[2] = find_tags($matches[2]);
      
      // Process the tags {%tag%}.
      $matches[2] = preg_replace_callback('/{%([^%]+)%}/sim', 'process_tags', $matches[2]);
      
      switch ( $matches[1] )
      {
        case 'a':
          $tag = 'div';
          
          break;
        case 'b':
          $tag = 'p';
          
          break;
        case 'c':
          $tag = 'b';
          
          break;
      }
      
      return '<'.$tag.'>'.$matches[2].'</'.$tag.'>';
    }
    
    function process_tags($matches)
    {
      switch ( $matches[1] )
      {
        case 'first':
          return 'Francois';
          
          break;
        case 'last':
          return 'Deschenes';
          
          break;
      }
    }
    
    //
    

    The resulting string will be: <div><p>I like Francois Deschenes a <b>lot</b>.</p></div>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this pattern all over the place to grab data from the database
I often use this code pattern: while(true) { //do something if(<some condition>) { break;
I use a pattern that looks something like this often. I'm wondering if this
Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand(GetSomething, new SqlConnection(Config.ConnectionString)) {
I want to use this pure HTML/CSS template for my ASP.NET website: http://sub3.tanguay.de I
I use this pattern to test for undefined and null values in ActionScript/Flex :
I want to use this pattern: SqlCommand com = new SqlCommand(sql, con); com.CommandType =
In answering this question , it got me thinking... I often use this pattern:
I had a regex, like so: (?<one-1>cat)|(?<two-2>dog)|(?<three-3>mouse)|(?<four-4>fish) When I tried to use this pattern
I normally use this pattern to iterate over object properties: for(var property in object)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.