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 相关文章推荐
PHP将整个网站生成HTML纯静态网页的方法总结
Feb 05 PHP
XAMPP安装与使用方法详细解析
Nov 27 PHP
php三元运算符知识汇总
Jul 02 PHP
PHP Imagick完美实现图片裁切、生成缩略图、添加水印
Feb 22 PHP
Yii2针对指定url的生成及图片等的引入方法小结
Jul 18 PHP
全面解析PHP验证码的实现原理 附php验证码小案例
Aug 17 PHP
php 删除指定文件夹的实例讲解
Jul 25 PHP
CodeIgniter框架钩子机制实现方法【hooks类】
Aug 21 PHP
PHP convert_uudecode()函数讲解
Feb 14 PHP
php use和include区别总结
Oct 13 PHP
Laravel重定向,a链接跳转,控制器跳转示例
Oct 22 PHP
PHP中用Trait封装单例模式的实现
Dec 18 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
codeigniter自带数据库类使用方法说明
2014/03/25 PHP
PHP判断数组是否为空的常用方法(五种方法)
2017/02/08 PHP
PHPMAILER实现PHP发邮件功能
2018/04/18 PHP
laravel请求参数校验方法
2019/10/10 PHP
php中yar框架实例用法讲解
2020/12/27 PHP
JS 页面自动加载函数(兼容多浏览器)
2009/05/18 Javascript
js猜数字小游戏的简单实现代码
2013/07/02 Javascript
jquery中each遍历对象和数组示例
2014/08/05 Javascript
drag-and-drop实现图片浏览器预览
2015/08/06 Javascript
javascript中checkbox使用方法简单实例演示
2015/11/17 Javascript
深入解析Vue 组件命名那些事
2017/07/18 Javascript
jQuery实现简单的下拉菜单导航功能示例
2017/12/07 jQuery
jQuery 同时获取多个标签的指定内容并储存为数组
2018/11/20 jQuery
JQuery中queue方法用法示例
2019/01/31 jQuery
JQuery属性操作与循环用法示例
2019/05/15 jQuery
微信小程序云开发修改云数据库中的数据方法
2019/05/18 Javascript
JS实现时间校验的代码
2020/05/25 Javascript
python使用urllib模块开发的多线程豆瓣小站mp3下载器
2014/01/16 Python
Python文件夹与文件的操作实现代码
2014/07/13 Python
Python深入学习之特殊方法与多范式
2014/08/31 Python
python切片及sys.argv[]用法详解
2018/05/25 Python
Python这样操作能存储100多万行的xlsx文件
2019/04/16 Python
selenium 安装与chromedriver安装的方法步骤
2019/06/12 Python
python中for循环把字符串或者字典添加到列表的方法
2019/07/20 Python
Pycharm github配置实现过程图解
2020/10/13 Python
python实现简单文件读写函数
2021/02/25 Python
GIVENCHY纪梵希官方旗舰店:高定彩妆与贵族护肤品
2018/04/16 全球购物
光信息科学与技术专业职业生涯规划
2014/03/13 职场文书
初二学习计划书范文
2014/04/27 职场文书
2014年实习期工作总结
2014/11/27 职场文书
简短的36句中秋节祝福信息语句
2019/09/09 职场文书
SQL实现LeetCode(176.第二高薪水)
2021/08/04 MySQL
JavaScript 原型与原型链详情
2021/11/02 Javascript
业余无线电通联Q语
2022/02/18 无线电
OpenCV实现常见的四种图像几何变换
2022/04/01 Python
python使用opencv对图像添加噪声(高斯/椒盐/泊松/斑点)
2022/04/06 Python