php下正则来匹配dede模板标签的代码


Posted in PHP onAugust 21, 2010

html代码

<!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>

php代码
<?php 
$str = file_get_contents("test.htm"); 
$skstr="{"; 
$skdstr="t:"; 
$sdstr="}"; 
//$reg="/{t:.*?(?:\/).*?}/si";//此无法匹配嵌套,只能一层 si s: 使前面的. 包括换行符 i:不区分大小写 
//$reg="/({t:*[^{]*\/}|{t:*[^}]*}([^{]|(?R))*{\/t:.*?})/si";无限嵌套都可截取. 
$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:'=>'');前缀 
$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; //原模板字符串 
$vs=explode('}',$v); //分割取得第一部分 {t:array typeid='1' 
$va=explode(' ',$vs[0]); //分割属性 " {t:array " , " typeid='1' " 
$vb=explode('.',$va[0]); //分割标签名和字段名 {t:global.title 
$rarr['tagname']=trim(strtr(strtolower($vb[0]),$glarr1));//过滤前缀{t: 取得标签名 array 或 golbal 
$rarr['tagfield']=strtr($vb[1],$glarr2); //取得字段名 title 
unset($vs[0]);$vsstr=implode('}',$vs); //去掉前缀,返回后半段: [field:typename/]{/t:array} 
$rarr['text']=preg_replace("/".$skstr."\/".$skdstr.$rarr['tagname']."(.*?)".$sdstr."/si",'',$vsstr); 
/*过滤后缀 {/t:array} 取得中间文本. 
可能出现,空格或者换行所以使用正则,也可直接使用strtr 替换 。 
{/t:array } 
{/t:array 
} 
strtr(strtolower($vsstr),array($skstr.'/'.$skdstr.$rarr['tagname'].$sdstr=>'')); 
*/ 
unset($va[0]); 
$attr=array(); 
foreach($va as $val){//循环记录属性 
$val=strtr($val,$glarr2); //typeid='1' 去掉 " ' / , typeid=1 
$vc=explode('=',$val); 
if(!empty($vc[0])){ 
$vc[0]=strtolower($vc[0]); //小写属性名 
if($vc[0]=="function"){ 
$rarr['function']=$vc[1]; //返回要执行的 html2text(@me) 
}else if($vc[0]=="name"){ 
$rarr['tagfield']=$vc[1]; //{t:field name='title' /} 
}else{ 
$attr[trim($vc[0])]=$vc[1]; //属性 $attr['typeid']=1; 
} 
} 
} 
$rarr['attr']=$attr; 
$CTags[$k]=$rarr; 
} 
echo "<pre>"; 
print_r($CTags); 
echo "</pre>"; 
?>

输出内容:
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 
) 
) 
)

测试代码打包下载
PHP 相关文章推荐
JS实现php的伪分页
May 25 PHP
php 获取一个月第一天与最后一天的代码
May 16 PHP
PHP面向对象分析设计的61条军规小结
Jul 17 PHP
PHP读取数据库并按照中文名称进行排序实现代码
Jan 29 PHP
Function eregi is deprecated (解决方法)
Jun 21 PHP
PHP swfupload图片上传的实例代码
Sep 30 PHP
使用phpQuery采集网页的方法
Nov 13 PHP
JavaScript实现滚动栏效果的方法
Apr 27 PHP
网页的分页下标生成代码(PHP后端方法)
Feb 03 PHP
PHP magento后台无法登录问题解决方法
Nov 24 PHP
php获取文章内容第一张图片的方法示例
Jul 03 PHP
ThinkPHP5+Layui实现图片上传加预览功能
Aug 17 PHP
php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)
Aug 21 #PHP
使用VisualStudio开发php的图文设置方法
Aug 21 #PHP
利用Memcached在php下实现session机制 替换PHP的原生session支持
Aug 21 #PHP
ThinkPHP中实例Model方法的区别说明
Aug 21 #PHP
PHP 数组基础知识小结
Aug 20 #PHP
一个简单php扩展介绍与开发教程
Aug 19 #PHP
php checkbox 取值详细说明
Aug 19 #PHP
You might like
《雄兵连》系列首部大电影《烈阳天道》:可能是因为期望值太高了
2020/08/18 国漫
php中有关合并某一字段键值相同的数组合并的改进
2015/03/10 PHP
php数字运算验证码的实现代码
2015/07/30 PHP
WordPress中限制非管理员用户在文章后只能评论一次
2015/12/31 PHP
PHP常见的6个错误提示及解决方法
2016/07/07 PHP
详解PHP处理字符串类似indexof的方法函数
2017/06/11 PHP
laravel 输出最后执行sql 附:whereIn的使用方法
2019/10/10 PHP
CL vs ForZe BO5 第三场 2.13
2021/03/10 DOTA
javascript 表单验证常见正则
2009/09/28 Javascript
jQuery创建插件的代码分析
2011/04/14 Javascript
修改好的jquery滚动字幕效果实现代码
2011/06/22 Javascript
jquery easyui combox一些实用的小方法
2013/12/25 Javascript
Jquery实现点击按钮,连续地向textarea中添加值的实例代码
2014/03/08 Javascript
JS根据生日算年龄的方法
2015/05/05 Javascript
Javascript控制div属性动态变化实例分析
2015/10/08 Javascript
bootstrap网页框架的使用方法
2016/05/10 Javascript
基于jquery二维码生成插件qrcode
2017/01/07 Javascript
ES6生成器用法实例分析
2017/04/10 Javascript
JS原生轮播图的简单实现(推荐)
2017/07/22 Javascript
Angular5中状态管理的实现
2018/09/03 Javascript
Python中操作文件之write()方法的使用教程
2015/05/25 Python
python调用c++传递数组的实例
2019/02/13 Python
python 图片二值化处理(处理后为纯黑白的图片)
2019/11/01 Python
如何提高python 中for循环的效率
2020/04/15 Python
浅谈sklearn中predict与predict_proba区别
2020/06/28 Python
10款最佳Python开发工具推荐,每一款都是神器
2020/10/15 Python
python制作抽奖程序代码详解
2021/01/15 Python
军用级手机壳,专为冒险而建:Zizo Wireless
2019/08/07 全球购物
奥地利手表、香水、化妆品和珠宝购物网站:Brasty.at
2021/01/17 全球购物
《燕子专列》教学反思
2014/02/21 职场文书
就业协议书盖章的注意事项
2014/09/28 职场文书
中学教师师德师风承诺书
2015/04/28 职场文书
大学生读书笔记范文
2015/07/01 职场文书
深入探讨opencv图像矫正算法实战
2021/05/21 Python
zabbix监控mysql的实例方法
2021/06/02 MySQL
GTX1650super好不好 gtx1650super显卡属于什么级别
2022/04/08 数码科技