PHP regular to match Dede template tag Code _php tutorial

Source: Internet
Author: User
HTML code
Copy CodeThe code is as follows:




<title></title>


{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= ' row= ' 10 '}

  • [field:title/]

  • ([Field:sendtime function= "mydate (' m-d ', @me)"/])


{/t:arclist}
{/t:channelartlist
}
{T:global.cfg_title
/}{t:var.cfg_title/}{t:global.cfg_webname/}{t:global.cfg_title/}{t:var.cfg_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
Copy CodeThe code is as follows:
$str = file_get_contents ("test.htm");
$skstr = "{";
$skdstr = "T:";
$sdstr = "}";
$reg = "/{t:.*?" (?: \ /).*?} /si ";//This cannot match nesting, only one layer Si s: Make the front. Include newline character 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 "
";p rint_r ($m [0]); echo"
"; exit (); * *
$CTags =array ();
$glarr 1=array ($skstr. $skdstr = ");//array (' {t: ' = '); prefix
$glarr 2=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); Split get first part {T:array typeid= ' 1 '
$va =explode (", $vs [0]); Split attribute "{T:array", "typeid= ' 1 '"
$vb =explode ('. ', $va [0]); Split-label signature and field name {T:global.title
$rarr [' tagname ']=trim (STRTR (strtolower ($vb [0]), $glarr 1));//filter prefix {t: Get label name Array or Golbal
$rarr [' Tagfield ']=strtr ($vb [1], $glarr 2); Get Field name title
unset ($vs [0]); $vsstr =implode ('} ', $vs); Remove the prefix and return to the second half: [Field:typename/]{/t:array}
$rarr [' Text ']=preg_replace ("/". $skstr. " \ ". $skdstr. $rarr [' tagname ']." (.*?)". $sdstr. " /si ",", $vsstr);
/* filter suffix {/t:array} to get intermediate text.
May appear, spaces or line breaks so use regular, or you can directly use STRTR substitution.
{/t:array}
{/t:array
}
STRTR (Strtolower ($VSSTR), Array ($skstr. ' /'. $skdstr. $rarr [' tagname ']. $sdstr =]);
*/
unset ($va [0]);
$attr =array ();
foreach ($va as $val) {//Looping record Properties
$val =strtr ($val, $glarr 2); Typeid= ' 1 ' Remove "'/, typeid=1
$VC =explode (' = ', $val);
if (!empty ($VC [0])) {
$VC [0]=strtolower ($VC [0]); Lowercase property name
if ($VC [0]== "function") {
$rarr [' function ']= $VC [1]; Returns 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 "


echo "
";
?>

Output content:
Copy CodeThe 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= ' 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= ' row= ' 10 '}
[field:title/]
([Field:sendtime function= "mydate (' m-d ', @me)"/])
{/t:arclist}
[attr] = = Array
(
[Row] = 6
)
)
[2] = = Array
(
[str] = {T:global.cfg_title
/}
[TagName] = Global
[Tagfield] = Cfg_title
[Text] = =
[attr] = = Array
(
)
)
[3] = = Array
(
[str] = {t:var.cfg_title/}
[TagName] = var
[Tagfield] = Cfg_title
[Text] = =
[attr] = = Array
(
)
)
[4] = = Array
(
[str] = {t:global.cfg_webname/}
[TagName] = Global
[Tagfield] = Cfg_webname
[Text] = =
[attr] = = Array
(
)
)
[5] = = Array
(
[str] = {t:global.cfg_title/}
[TagName] = Global
[Tagfield] = Cfg_title
[Text] = =
[attr] = = Array
(
)
)
[6] = = Array
(
[str] = {t:var.cfg_title/}
[TagName] = var
[Tagfield] = Cfg_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
)
)
[Ten] + = Array
(
[str] = {t:field.title function= ' html2text (@me) '}{/t:field
}
[TagName] = Field
[Tagfield] = title
[Text] = =
[Function] = Html2text (@me)
[attr] = = Array
(
)
)
[one] = = Array
(
[str] = {t:array name= ' Asdas '}{/t:array
}
[TagName] = = Array
[Tagfield] = Asdas
[Text] = =
[attr] = = Array
(
)
)
[+] = 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
)
)
[+] = Array
(
[str] = {t:channel typeid= ' 1 '} [Field:id/] [Field:typename/]{/t:channel}
[TagName] = Channel
[Tagfield] =
[Text] = [Field:id/] [field:typename/]
[attr] = = Array
(
[typeID] = 1
)
)
[+] = Array
(
[str] = {t:channel typeid= ' 1 '}[field:id/]
[field:typename/] {/t:channel
}
[TagName] = Channel
[Tagfield] =
[Text] = [Field:id/]
[field:typename/]
[attr] = = Array
(
[typeID] = 1
)
)
[+] = Array
(
[str] = {T:channel}[field:typename/]{/t:channel
}
[TagName] = Channel
[Tagfield] =
[Text] = [field:typename/]
[attr] = = Array
(
)
)
[+] = Array
(
[str] = {t:channel typeid= ' 1 ' row= '}[field:userid/]
[Field:scores/]
{/t:channel
}
[TagName] = Channel
[Tagfield] =
[Text] = [Field:userid/]
[Field:scores/]
[attr] = = Array
(
[typeID] = 1
[Row] = 20
)
)
)

Test Code package download

http://www.bkjia.com/PHPjc/322269.html www.bkjia.com true http://www.bkjia.com/PHPjc/322269.html techarticle The HTML code Copy Code code is as follows:! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "HTML xmlns=" http://www ....

  • 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.