html用模板表达式,正则表达式,用于解析HTML中的模板块

我的HTML文件中有以下块

{firstname} {lastname}

上面的

user_details是一个包含如下数据的数组:

$user_details = array(

0 => array('firstname' => 'Hercules', 'lastname' => 'Zeus'),

1 => array('firstname' => 'Jesus', 'lastname' => 'Christ'),

2 => array('firstname' => 'John', 'lastname' => 'Smith'),

3 => array('firstname' => 'Goerge', 'lastname' => 'Bush')

);

还有其他块具有不同的BEGIN / FINSIH数组名称和不同的{elements}所以,我需要一个正则表达式,它将遍历整个文件并查找像那样的块,然后遍历其中的每个值并用实际值替换它们......

我的最终输出应该是:

Hercules Zeus

Jesus Christ

John Smith

George Bush

标记未修复,在某些块中我使用等。

我现在有了这段代码:

$search_in = file_get_contents('path/to/my/html/file/');

$search_for = "#(.+?)#si";

if (!preg_match($search_for, $search_in, $return)) {

return NULL;

}

$return = preg_replace("#", "", $return[0]);

$return = preg_replace("#si", "", $return);

return var_dump($return);

//Look for anything enclosed in curly brackes like so {something}

if(!preg_match_all("/{([^}]*)}/", $search_in, $matches)) {

return NULL;

}

//Return all occurrences of {something} in an array for use later

foreach($matches[0] as $value) {

$_args[] = $value;

}

但第一个不是var_dump任何数据,只有NULL,而且我知道上面的所有内容都是有效数据。我收到这些错误:

Notice: Undefined index: firstname in /home/content/v/i/r... on line 96

Notice: Undefined index: lastname in /home/content/v/i/r... on line 96

我不想使用像codeIgniter,cakePHP,Zend等框架。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值