PHP to match the Dede template tag code _php tips

Source: Internet
Author: User
Tags explode php code tag name tagname
HTML code
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
{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 '
}
<dl>
<dt>{t:field name= ' TypeName '/}{t:field.typename/}</dt>
<dd>
{t:arclist titlelen= ' row= ' 10 '}
<ul class= ' Autod ' >
<li>[field:title/]</li>
<li> ([Field:sendtime function= "mydate (' m-d ', @me)"/]) </li>
</ul>
{/t:arclist}
</dl>{/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
}
</body>

PHP code
Copy Code code as follows:

<?php
$str = file_get_contents ("test.htm");
$skstr = "{";
$skdstr = "T:";
$sdstr = "}";
$reg = "/{t:.*" (?: \ /).*?} /si "//This cannot match the nesting, only one layer Si s: Make the front. Include line breaks 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 "<pre>";p rint_r ($m [0]); echo "</pre>"; 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 to get the 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 tag 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 of the paragraph: [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, a space or a newline so use regular, you can also directly use STRTR replacement.
{/t:array}
{/t:array
}
STRTR (Strtolower ($VSSTR), Array ($skstr. ' /'. $skdstr. $rarr [' tagname ']. $sdstr => '));
*/
unset ($va [0]);
$attr =array ();
foreach ($va as $val) {//Circular logging 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]; Property $attr [' typeid ']=1;
}
}
}
$rarr [' attr ']= $attr;
$CTags [$k]= $rarr;
}
echo "<pre>";
Print_r ($CTags);
echo "</pre>";
?>

Output content:
Copy Code code 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
(
)
)
[of] => 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
)
)
[A] => 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
)
)
[A] => 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
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.