PHP regular to match the dede template tag code

  • 2020-03-31 21:06:29
  • OfStack

The HTML code
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
</head> 
<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='42' 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> 
</html> 

The PHP code
 
<?php 
$str = file_get_contents("test.htm"); 
$skstr="{"; 
$skdstr="t:"; 
$sdstr="}"; 
//$reg="/{t:.*?(?:/).*?}/si";// This does not match nesting , Only a layer of  si s:  Make the previous .  Includes newline characters  i: Case insensitive  
//$reg = "/ ({t: * [^ {] *  /} | {t: * *} [^}] ([^ {] | (? R)) * { / t:. *? })/si "; Unlimited 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>";print_r($m[0]);echo "</pre>";exit();*/ 
$CTags=array(); 
$glarr1=array($skstr.$skdstr=>'');//Array (' {t: => "); The 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); //{t:array typeid='1'
$va=explode(' ',$vs[0]); //{t:array ", "typeid='1'"
$vb=explode('.',$va[0]); //Split the tag name and the field name {t:global.title
$rarr['tagname']=trim(strtr(strtolower($vb[0]),$glarr1));//Filter prefix {t: gets the tag name array or golbal
$rarr['tagfield']=strtr($vb[1],$glarr2); //Gets the field name title
unset($vs[0]);$vsstr=implode('}',$vs); //[field:typename/]{/t:array}
$rarr['text']=preg_replace("/".$skstr."/".$skdstr.$rarr['tagname']."(.*?)".$sdstr."/si",'',$vsstr); 
/* Filtering the suffix  {/t:array}  Get the intermediate text . 
 Can appear, space or line break so use regular, can also be used directly strtr  replace   .  
{/t:array } 
{/t:array 
} 
strtr(strtolower($vsstr),array($skstr.'/'.$skdstr.$rarr['tagname'].$sdstr=>'')); 
*/ 
unset($va[0]); 
$attr=array(); 
foreach($va as $val){//Looping property
$val=strtr($val,$glarr2); //Typeid ='1' remove "' /, typeid=1
$vc=explode('=',$val); 
if(!empty($vc[0])){ 
$vc[0]=strtolower($vc[0]); //Lowercase attribute name
if($vc[0]=="function"){ 
$rarr['function']=$vc[1]; //Return html2text(@me) to execute
}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 "<pre>"; 
print_r($CTags); 
echo "</pre>"; 
?> 

Output content:
 
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.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 
) 
) 
[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 
) 
) 
) 

(link: http://xiazai.jb51.net/201008/yuanma/cdede.rar)

Related articles: