Php regular expression to match the dede template tag code

Source: Internet
Author: User
Tags php regular expression
Use regular expressions to match dede tags. For more information about template tags like dedecms, see dedecms template tags. Html code

The code is as follows:









{T: arc row = '10 '}
{T: field. title /}
{T: list row = '5 '}
{T: field. title /}
{T: list row = '5 '}
{T: field. title /}
{/T: list} {t: field. title /}
{/T: list} {t: field. title /}
{/T: arc}
{T: channelartlist
Row = '6'
}


{T: field name = 'typename'/} {t: field. typename /}


{T: arclist titlelen = '42' row = '10 '}

  • [Field: title/]

  • ([Field: sendtime function = "MyDate ('M-D', @ me)"/])


{/T: arclist}
{/T: channelartlist
}
{T: global. pai_title
//} {T: var. cmd_title/} {t: global. cmd_webname/} {t: global. cmd_title/} {t: var. cmd_title /}
{T: field. title function = "Html2Text (@ me )"/}
{T: include
Filename = "header.htm "/}
{T: include filename = "header.htm"
/}
{T: field. title function = 'html2text (@ me) '} {/t: field
}
{T: array name = 'asdas '} {/t: array
}
{T: channel typeid = '1' row = '15 '}
[Field: id/] [field: typename/]
{/T: channel
}
{T: channel typeid = '1'} [field: id/] [field: typename/] {/t: channel}
{T: channel typeid = '1'} [field: id/]
[Field: typename/] {/t: channel
} Sd
{T: channel} [field: typename/] {/t: channel
}
{T: channel typeid = '1' row = '20'} [field: userid/]
[Field: scores/]
{/T: channel
}



Php code

The code is as follows:


$ Str = file_get_contents ("test.htm ");
$ Skstr = "{";
$ Skdstr = "t :";
$ Sdstr = "}";
// $ Reg = "/{t :.*? (? :\/).*?} /Si "; // This cannot match nesting. only one layer of si s is allowed: make the previous. include line break I: case-insensitive
// $ Reg = "/({t: * [^ {] * \/} | {t: * [^}] *} ([^ {] | (? R) * {\/t :.*?}) /Si "; infinite nesting can be intercepted.
$ Reg = '/('. $ skstr. $ skdstr. '* [^ '. $ skstr. '] * \/'. $ sdstr. '| '. $ skstr. $ skdstr. '* [^ '. $ sdstr. '] *'. $ sdstr. '([^ '. $ skstr. '] | (? R) * '. $ skstr.' \/'. $ skdstr .'.*? '. $ Sdstr.')/si ';
Preg_match_all ($ reg, $ str, $ m );
/* Echo"

";print_r($m[0]);echo "
"; Exit ();*/
$ CTags = array ();
$ Glarr1 = array ($ skstr. $ skdstr => ''); // array ('{t:' =>''); prefix
$ Glarr2 = array ('"' =>'', '\ ''=>'',' $ '=> '','/'=> '');
/*
{T: array typeid = '1'} [field: typename/] {/t: array}
{T: global. title function = "html2text (@ me )"/}
*/
Foreach ($ m [0] as $ k => $ v ){
$ Rarr = array ();
$ Rarr ['str'] = $ v; // original template string
$ Vs = explode ('}', $ v); // obtain the first part of the split {t: array typeid = '1'
$ Va = explode ('', $ vs [0]); // split attributes" {t: array "," typeid = '1 '"
$ Vb = explode ('.', $ va [0]); // split the tag name and field name {t: global. title
$ Rarr ['tagname'] = trim (strtolower ($ vb [0]), $ glarr1); // filter the prefix {t: get the tag name array or golbal
$ Rarr ['tagfield'] = strtr ($ vb [1], $ glarr2); // Obtain the field name title
Unset ($ vs [0]); $ vsstr = implode ('}', $ vs); // remove the prefix and return the second half: [field: typename/] {/t: array}
$ Rarr ['text'] = preg_replace ("/". $ skstr. "\/". $ skdstr. $ rarr ['tagname']. "(. *?) ". $ Sdstr."/si ",'', $ vsstr );
/* Filter the suffix {/t: array} to obtain the intermediate text.
It may appear, with spaces or line breaks. Therefore, use regular expressions or replace strtr directly.
{/T: array}
{/T: array
}
Strtr (strtolower ($ vsstr), array ($ skstr. '/'. $ skdstr. $ rarr ['tagname']. $ sdstr => ''));
*/
Unset ($ va [0]);
$ Attr = array ();
Foreach ($ va as $ val) {// Loop Record attributes
$ Val = strtr ($ val, $ glarr2); // typeid = '1' remove "'/, typeid = 1
$ Vc = explode ('=', $ val );
If (! Empty ($ vc [0]) {
$ Vc [0] = strtolower ($ vc [0]); // Lower case attribute name
If ($ vc [0] = "function "){
$ Rarr ['function'] = $ vc [1]; // return the html2text (@ me) to be executed)
} Else if ($ vc [0] = "name "){
$ Rarr ['tagfield'] = $ vc [1]; // {t: field name = 'title '/}
} Else {
$ Attr [trim ($ vc [0])] = $ vc [1]; // attribute $ attr ['typeid'] = 1;
}
}
}
$ Rarr ['attr '] = $ attr;
$ CTags [$ k] = $ rarr;
}
Echo"
"; 
print_r($CTags);
echo "
";
?>


Output Content:

The code is as follows:


Array
(
[0] => Array
(
[Str] =>{ t: arc row = '10 '}
{T: field. title /}
{T: list row = '5 '}
{T: field. title /}
{T: list row = '5 '}
{T: field. title /}
{/T: list} {t: field. title /}
{/T: list} {t: field. title /}
{/T: arc}
[Tagname] => arc
[Tagfield] =>
[Text] =>
{T: field. title /}
{T: list row = '5 '}
{T: field. title /}
{T: list row = '5 '}
{T: field. title /}
{/T: list} {t: field. title /}
{/T: list} {t: field. title /}
[Attr] => Array
(
[Row] => 10
)
)
[1] => Array
(
[Str] => {T: channelartlist
Row = '6'
}
{T: field name = 'typename'/} {t: field. typename /}
{T: arclist titlelen = '42' row = '10 '}
[Field: title/]
([Field: sendtime function = "MyDate ('M-D', @ me)"/])
{/T: arclist}
{/T: channelartlist
}
[Tagname] => channelartlist
[Tagfield] =>
[Text] =>
{T: field name = 'typename'/} {t: field. typename /}
{T: arclist titlelen = '42' row = '10 '}
[Field: title/]
([Field: sendtime function = "MyDate ('M-D', @ me)"/])
{/T: arclist}
[Attr] => Array
(
[Row] => 6
)
)
[2] => Array
(
[Str] => {t: global. short_title
/}
[Tagname] => global
[Tagfield] => pai_title
[Text] =>
[Attr] => Array
(
)
)
[3] => Array
(
[Str] =>{ t: var. cmd_title /}
[Tagname] => var
[Tagfield] => pai_title
[Text] =>
[Attr] => Array
(
)
)
[4] => Array
(
[Str] => {t: global. cmd_webname /}
[Tagname] => global
[Tagfield] => pai_webname
[Text] =>
[Attr] => Array
(
)
)
[5] => Array
(
[Str] => {t: global. short_title /}
[Tagname] => global
[Tagfield] => pai_title
[Text] =>
[Attr] => Array
(
)
)
[6] => Array
(
[Str] =>{ t: var. cmd_title /}
[Tagname] => var
[Tagfield] => pai_title
[Text] =>
[Attr] => Array
(
)
)
[7] => Array
(
[Str] => {t: field. title function = "Html2Text (@ me )"/}
[Tagname] => field
[Tagfield] => title
[Text] =>
[Function] => Html2Text (@ me)
[Attr] => Array
(
)
)
[8] => Array
(
[Str] => {t: include
Filename = "header.htm "/}
[Tagname] => include
[Tagfield] =>
[Text] =>
[Attr] => Array
(
[Filename] => header.htm
)
)
[9] => Array
(
[Str] => {t: include filename = "header.htm"
/}
[Tagname] => include
[Tagfield] =>
[Text] =>
[Attr] => Array
(
[Filename] => header.htm
)
)
[10] => Array
(
[Str] => {t: field. title function = 'html2text (@ me) '} {/t: field
}
[Tagname] => field
[Tagfield] => title
[Text] =>
[Function] => Html2Text (@ me)
[Attr] => Array
(
)
)
[11] => Array
(
[Str] => {t: array name = 'asdas '} {/t: array
}
[Tagname] => array
[Tagfield] => asdas
[Text] =>
[Attr] => Array
(
)
)
[12] => Array
(
[Str] =>{ t: channel typeid = '1' row = '15 '}
[Field: id/] [field: typename/]
{/T: channel
}
[Tagname] => channel
[Tagfield] =>
[Text] =>
[Field: id/] [field: typename/]
[Attr] => Array
(
[Typeid] => 1
[Row] => 15
)
)
[13] => Array
(
[Str] => {t: channel typeid = '1'} [field: id/] [field: typename/] {/t: channel}
[Tagname] => channel
[Tagfield] =>
[Text] => [field: id/] [field: typename/]
[Attr] => Array
(
[Typeid] => 1
)
)
[14] => Array
(
[Str] =>{ t: channel typeid = '1'} [field: id/]
[Field: typename/] {/t: channel
}
[Tagname] => channel
[Tagfield] =>
[Text] => [field: id/]
[Field: typename/]
[Attr] => Array
(
[Typeid] => 1
)
)
[15] => Array
(
[Str] => {t: channel} [field: typename/] {/t: channel
}
[Tagname] => channel
[Tagfield] =>
[Text] => [field: typename/]
[Attr] => Array
(
)
)
[16] => Array
(
[Str] =>{ t: channel typeid = '1' row = '20'} [field: userid/]
[Field: scores/]
{/T: channel
}
[Tagname] => channel
[Tagfield] =>
[Text] => [field: userid/]
[Field: scores/]
[Attr] => Array
(
[Typeid] => 1
[Row] => 20
)
)
)


Package and download test code
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.