php模板-
"home-pg-feed-video" =>
"<div class='"row home-pg-feed-item pl0 pr0 ml0 mr0'" data-type='"video'">" .
"<div class='"col-lg-4 col-xs-4 pl0'">" .
"<a href='"/clinical-dialogue.php?v={{ url }}'"><img src='"{{ thumb-sm }}'" alt='"img'"/></a>" .
"</div>" .
"<div class='"col-lg-8 col-xs-8 pr0 '">" .
"<h4 class='"mb5'">{{ title }}</h4>" .
"<a href='"/clinical-dialogue.php?v={{ url }}'" class='"watch-link'">Watch the video</a>" .
"<p>{{ summary }}</p>" .
"</div>" .
"</div>"
带有"img"标签的链接正确输出-
<a href="/clinical-dialogue.php?v=Dirk_Arnold_Patient_Selection_3rd_Line_&_Sequencing_Final_Branded-video.php"><img src="images/vid_thumbs/sm/Dirk_Arnold_Branded.png" alt="img"></a>
但是"观看视频"的第二个链接输出-
<a href="/clinical-dialogue.php?v={{ url }}" class="watch-link">Watch the video</a>
我使用模板
的标记<div class="col-lg-8 col-xs-8 col-lg-feed">
<h3 class="feed-name mt0">Featured Clinical Dialogue</h3>
<?php
for($i = 0; $i < count($featureData); $i++){
$item = $featureData[$i];
if($item["type"] === "video"){
$template = $templates["home-pg-feed-video"];
$html = $template;
foreach($item as $key => $value){
if($key == "topics") {
$value = implode(", ", $value);
}
$html = preg_replace("/{{ " . $key . " }}/", $value, $html, 1);
}
echo $html;
}
}
?>
</div>
您的preg_replace
行中有"1",这限制了匹配的数量(即:"url")到1。使用'-1'表示没有限制。
$html = preg_replace("/{{ " . $key . " }}/", $value, $html, -1);
或
$html = preg_replace("/{{ " . $key . " }}/", $value, $html);
但是在您的情况下,不需要regex。使用str_replace,因为它更快:
$html = str_replace("{{ " . $key . " }}", $value, $html);