我的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等框架。