Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的Foreach模板标记_Php_Regex_Preg Match - Fatal编程技术网

php中的Foreach模板标记

php中的Foreach模板标记,php,regex,preg-match,Php,Regex,Preg Match,我编写了以下代码来替换模板标记。它适用于1项,但我正在寻找一个解决方案,以取代列出的每一项。代码仅将模板标记替换为项目2的内容 $template = ' <div style="border:1px solid blue;padding:10px;"> <h1>[item_title]</h1> <p>[item_text]</p> <a href="[item_link]">[item_link]</a> &

我编写了以下代码来替换模板标记。它适用于1项,但我正在寻找一个解决方案,以取代列出的每一项。代码仅将模板标记替换为项目2的内容

$template = '
<div style="border:1px solid blue;padding:10px;">
<h1>[item_title]</h1>
<p>[item_text]</p>
<a href="[item_link]">[item_link]</a>
</div>

<div style="border:1px solid blue;padding:10px;">
<h1>[item_title]</h1>
<p>[item_text]</p>
<a href="[item_link]">[item_link]</a>
</div>
';

/// ITEM 1 
$title   = 'title 1';
$text    = 'text text text';
$link    = 'http://www.google.com';

/// ITEM 2 
$title   = 'title 2';
$text    = 'text2 text2 text2';
$link    = 'http://www.google.com';

$regex = array(
'/\[\item_title\]/is' => $title,
'/\[\item_text\]/is' => $text,
'/\[\item_link\]/is' => $link,
);


echo preg_replace(array_keys($regex), array_values($regex), $template);
$template=”
[项目名称]
[项目(正文)]

[项目名称] [项目(正文)]

'; ///项目1 $title='title 1'; $text='text'; $link='1http://www.google.com'; ///项目2 $title='title 2'; $text='text2text2text2'; $link='1http://www.google.com'; $regex=数组( '/\[\item\u title\]/是'=>$title, '/\[\item\u text\]/是'=>$text, '/\[\item\u link\]/是'=>$link, ); echo preg_replace(数组_键($regex)、数组_值($regex)、$template);
这应该可以做到:

$template = '<div style="border:1px solid blue;padding:10px;"><h1>[item_title]</h1><p>[item_text]</p><a href="[item_link]">[item_link]</a></div>';

$items = array(
    array(
        "title" => "title 1",
        "text" => "text text text",
        "link" => "http://www.google.com",
    ),
    array(
        "title" => "title 2",
        "text" => "text text text",
        "link" => "http://www.google.com",
    )
);
foreach($items as $item) {
    $regex = array(
        '/\[\item_title\]/is' => $item["title"],
        '/\[\item_text\]/is' => $item["text"],
        '/\[\item_link\]/is' => $item["link"],
    );
    echo preg_replace(array_keys($regex), array_values($regex), $template);
}
$template='[item_title][item_text]

; $items=数组( 排列( “标题”=>“标题1”, “文本”=>“文本”, “链接”=>“http://www.google.com", ), 排列( “标题”=>“标题2”, “文本”=>“文本”, “链接”=>“http://www.google.com", ) ); foreach($items作为$item){ $regex=数组( '/\[\item\u title\]/is'=>$item[“title”], '/\[\item\u text\]/是'=>$item[“text”], '/\[\item\u link\]/is'=>$item[“link”], ); echo preg_replace(数组_键($regex)、数组_值($regex)、$template); }
更改此选项

echo preg_replace(array_keys($regex), array_values($regex), $template);


在foreach内部使用
foreach($regex as $keys => $values)
{
    $template = preg_replace($keys, $values, $template);
}

echo $template;