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 3784856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:26:24+00:00 2026-05-19T11:26:24+00:00

I am working with template files that contain lines like these: [field name=main_div type=’smallblock’

  • 0

I am working with template files that contain lines like these:

[field name="main_div" type='smallblock' required="yes"]
[field type='bigblock' color="red" name="inner_div"]
[field name="btn" type='button' caption='Submit']

mixed with HTML lines.

It’s pseudocode for html code generation according to attribute values.

I have limited set of attributes, but don’t control their order in string and presence of all of them. Sometimes “required” attribute is set, sometimes is missed, for example.

What is the easiest and convenient way to parse such strings, so I can work with attributes as associative array?

Regular expression, finite state machine, get substring from [ to ], explode by space and explode by equal sign?

Looking for advice or simple piece of code that can work with provided example.

  • 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-19T11:26:24+00:00Added an answer on May 19, 2026 at 11:26 am

    Regular expressions. While you could write a parser for schemes like this, it’s overkill and provides no resiliency against garbled tokens.

    The trick is to use two regular expressions, one for finding the [field] tokens and a second to split out the attributes.

    preg_replace_callback('/\[(\w+)(\s+\w+=\pP[^"\']*\pP)*\]/', "block", $);
    
    function block($match) {
    
        $field = $match[1];
    
        preg_match_all('/(\w+)=\pP([^"\']+)\pP/', $match[2], $attr);
        $attr = array_combine($attr[1], $attr[2]);
    
        // ...
        return $html;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working up a custom Xcode template, and I'd like to change the way
I am working on a VSTO Excel 2010 Macro Enabled Template Project, that I
I am working on a Java app that uses Spring IoC and JDBC Template
I'm testing the new template for a website that I'm working for, and on
The following isn't working as intended: <xsl:template match=xs:complexType> <xsl:param name=prefix /> <xsl:if test=$prefix='core'> <xsl:variable
I am working on a Silverlight application that has resource files that define styles
I am working on a Excel 2007 workbook that will contain a macro to
We have an editor template that contains approx 40 lines of jquery. I tried
I'm working on an installer that needs to add several files to AVR Studio
How do I set the path in for coldfusion file/template so that files referenced

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.