1

The php template -

"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>"

The link with "img" tag inside outputs correctly -

<a href="/clinical-dialogue.php?v=Dirk_Arnold_Patient_Selection_3rd_Line_&amp;_Sequencing_Final_Branded-video.php"><img src="images/vid_thumbs/sm/Dirk_Arnold_Branded.png" alt="img"></a>

But the second link of "watch video" outputs -

<a href="/clinical-dialogue.php?v={{ url }}" class="watch-link">Watch the video</a>

The markup where I am using the template

<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>

1 Answer 1

1

You have "1" in your preg_replace line which limits the number of matches (i.e. "url") to just 1. Use '-1' to not have a limit.

So

$html = preg_replace("/{{ " . $key . " }}/", $value, $html, -1);

Or

$html = preg_replace("/{{ " . $key . " }}/", $value, $html);

However in your case there is no need for regex. Use str_replace as it is faster:

$html = str_replace("{{ " . $key . " }}", $value, $html);
Sign up to request clarification or add additional context in comments.

13 Comments

Did not change anything. @yashua
Weird. I just ran your template through the preg_replace with -1 and it worked.
Wierd indeed. What am I missing ... @yashua
"<p>{{ summary }}</p>" . Which comes after the link outputs correctly, but the link before the does not. @yashua
With "1" as the limit I would expect all your replacement to work except for the second "url". Removing the "1" (or using -1) should effectively cause all {{ url }} to change. Try removing the first one from the template to see if the second one works and also to make sure there are no weird invisible characters in the {{ url }} expression try copying and pasting the first {{ url }} to the second one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.