繁体   English   中英

使用正则表达式解析HTML中的模板块

[英]Regular expression to parse template blocks in HTML

我的HTML文件中包含以下代码块

<!-- BEGIN user_details -->
<h1>{firstname} {lastname}</h1>
<!-- FINISH user_details -->

上面的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},因此,我需要一个正则表达式,它将遍历整个文件并查找类似的块,然后遍历文件中的每个值并替换它们带有实际值...

我的最终输出应该是:

<!-- user_details -->
<h1>Hercules Zeus</h1>
<h1>Jesus Christ</h1>
<h1>John Smith</h1>
<h1>George Bush</h1>
<!-- /userdetails -->

<h1>…</h1>标记不固定,在某些块中,我使用<li></li>等。

我现在有以下代码:

$search_in = file_get_contents('path/to/my/html/file/');
$search_for = "#<!-- BEGIN (.*?) -->(.+?)<!-- FINISH (.*?) -->#si";
if (!preg_match($search_for, $search_in, $return)) {
    return NULL;
}

$return = preg_replace("#<!-- BEGIN (.*?) -->", "", $return[0]);
$return = preg_replace("<!-- FINISH (.*?) -->#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之类的框架。

function expand_template_blocks($matches) {
    $name = $matches[1];
    if (!isset($GLOBALS[$name])) {
        trigger_error("$name block has no input values");
        return "<!-- ERROR: $name -->";
    }
    $values = $GLOBALS[$name];
    $template_body = $matches[2];
    $expanded = '';
    foreach ($values as $item) {
        $expanded .= preg_replace_callback(
            "#{([^}]*)}#",
            function($m) use ($item) {
                // If an unknown key is found, the placeholder is left untouched.
                return isset($item[$m[1]]) ? $item[$m[1]] : $m[0];
            },
            $template_body
        );
    }
    return "<!-- $name -->$expanded<!-- /$name -->";
}
function process_file($path) {
    $source = file_get_contents($path);
    $block_template = '#<!-- BEGIN (.*?) -->(.+?)<!-- FINISH \1 -->#si';
    return preg_replace_callback($block_template, 'expand_template_blocks', $source);
}

echo process_file('path/to/my/html/file/');

每个块模板匹配项都传递给expand_template_blocks并由其输出替换。 块名称用于获取该名称的全局数组。 此数组的每个项目都用作关联数组以实例化模板-占位符被该项目中的相应值替换。 所有模板实例都是串联的。

由于使用匿名函数,因此需要PHP 5.3.0。 使用create_function可以解决此问题。

因此,看来您基本上需要分别匹配{firstname}{lastname} ,然后根据您的数组插入值,对吧?

如果是这样,那是一个非常基本的正则表达式,也许太基本了 ...

这是您要使用的表达方式...

(\\{firstname\\}).*?(\\{lastname\\})

如果需要指定只能在<h1><li>标记中找到它们:

<(?:h1|li)[^>]*>(\\{firstname\\}).*?(\\{lastname\\})</(?:h1|li)>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM