How to combine text inside the beginning and closing curly brackets, tags and specified attributes

I am implementing the plugin code for my CMS system. Something like short code, but will be used in many scenarios. I need a case where the administrator writes his code as follows:

Example 1:

{COMMAND_NAME}Strings of texts that conatains htmltags,symbols,just anything{/COMMAND_NAME}

Example 2

{COMMAND_NAME}

Example 3

{COMMAND_NAME{attriute1=value attribute2=value}}

Example 4

{COMMAND_NAME{attriute1=value attribute2=value}}Strings of anything including texts, htmltags and anything at all {/COMMAND_NAME}

Regex may match the above line. Get COMMAND_NAME, find the text between them and get a closure {/COMMAND_NAME}from one regex pattern.

In a regular expression, I want to capture COMMAND_NAMEattributes, if any, text between them, if it {COMMAND_NAME}has closing {/COMMAND_NAME}and closing {/COMMAND_NAME}, if provided.

Look at what I have done so far, and I will do an incomplete result.

$regex = #\{(RAW|ACCESS|DWNLINK|MODL)[\{]{0,1}([\w\W\s]*?)\}{0}\}([\w\s]+)([\{/RAW|ACCESS|DWNLINK|MODL]*)\}#i

$strings = '<div class="blog-list-item blog"><header class="entry-title">
        <h1>Welcome to our website</h1>
    </header><article id="entry-72" class="entry post-72 page et-bg-layout-dark et-white-bg"><div class="jumbotron row">
<div class="col-md-8">
<ul>
<li>You have a pending job on your neck?&hellip;</li>
<li>Do your company need a website makeover ?&hellip;</li>
<li>Or a competitive web application ? ?&hellip;</li>
<li>Do you need a customized plugin, or a tweak ?&hellip;</li>
<li>Maybe you want a personal website ?&hellip;</li>
<li>Or a graphic for your new project ?&hellip;</li>
</ul>
<div class="bg-primary well">
<h4 class="text-center text-white shadow">Track your project as we work it         to perfection...</h4>
</div>
</div>
<div class="pull-right col-md-4">
<h4 class="bg-primary text-white well">Other services we offer</h4>
{ACCESS{type=500}}
<ul>
<li>SEO work for an existing website or new</li>
<li>Bulk SMS</li>
<li>E-currency exchange</li>
<li>Facebook AD</li>
<li>Google AD</li>
</ul>
{/ACCESS}</div>
{RAW{say=email,access=500}} {RAW} <a class="btn button large tall green"     href="client-area">Place new Job now as we deliver at the quickest   <em>reasonable time</em></a>{/RAW}</div></article></div>';

And doing a php var_dump, gives the following result:
array(5) {
  [0]=>
  array(1) {
    [0]=>
    string(224) "{ACCESS{type=500}}
<ul>
<li>SEO work for an existing website or new</li>
<li>Bulk SMS</li>
<li>E-currency exchange</li>
<li>Facebook AD</li>
<li>Google AD</li>
</ul>
{/ACCESS}</div>
{RAW{say=email,access=500}} {RAW}"
  }
  [1]=>
  array(1) {
    [0]=>
    string(6) "ACCESS"
  }
  [2]=>
  array(1) {
    [0]=>
    string(209) "type=500}}
<ul>
<li>SEO work for an existing website or new</li>
<li>Bulk SMS</li>
<li>E-currency exchange</li>
<li>Facebook AD</li>
<li>Google AD</li>
</ul>
{/ACCESS}</div>
{RAW{say=email,access=500}"
  }
  [3]=>
  array(1) {
    [0]=>
    string(1) " "
  }
  [4]=>
  array(1) {
    [0]=>
    string(4) "{RAW"
  }
}

, . , COMMAND_NAME, , , , {COMMAND_NAME} {/COMMAND_NAME} {/COMMAND_NAME}, . , inline {COMMAND_NAME}, {COMMAND_NAME} {/COMMAND_NAME}, {COMMAND_NAME{attr1=value attr2=value2}} .

+1
1

, :

$regex = '~

#opening tag
\{(RAW|ACCESS|DWNLINK|MODL|\w+)
 #optional attributes
 (?>
     \{   ([^}]*)   }
 )?

}


#optional text and closing tag
(?:
    (   #text:= any char except "{", or a "{" not followed by /commandname
        [^{]*+
        (?>\{(?!/?\1[{}])[^{]*)*+
    )

    #closing tag
    (   \{/\1}   )
)?

~ix';

regex101


, :

, /x ( ), #comments.

, \w+ :

\{(RAW|ACCESS|DWNLINK|MODL|\w+)

[\{]{0,1}([\w\W\s]*?)\}{0}, . (?> group )? (. ), ( ? quantifier).

 (?>
     \{   ([^}]*)   }
 )?

, .

[\w\s]+ , , . .*?, . , , :

    (   #text:= any char except "{", or a "{" not followed by /commandname
        [^{]*+
        (?>\{(?!/?\1[{}])[^{]*)*?
    )

, , \1, , 1 ( ):

\{/\1}

:

  • , "te}xt", .
+1

All Articles