2

I have this code:

<body>
    <label id="Label1052">The loop is below</label>
    <img id="Image733" src="logo-2.png">
    <!--the loop--><div id="theLoop">
        <label id="Label736">{title}</label>
        <label id="Label737">{date}</label>
        <label id="Label739">{content}</label>
    </div><!--the loop-->
</body>

Expected output:

<body>
    <label id="Label1052">The loop is below</label>
    <img id="Image733" src="logo-2.png">
    <!--the loop--><div id="theLoop">
        <label id="Label736">Post 1</label>
        <label id="Label737">Jan 1</label>
        <label id="Label739">The content</label>
    </div>
    <div id="theLoop">
        <label id="Label736">Post 2</label>
        <label id="Label737">Jan 5</label>
        <label id="Label739">The content</label>
    </div>
    <div id="theLoop">
        <label id="Label736">Post 3</label>
        <label id="Label737">Jan 6</label>
        <label id="Label739">The content</label>
    </div><!--the loop-->
</body>

Here is my PHP:

// contains the content above
$markup = "<body>...</body>";

// find the loop and get the contents that has tokens
$match = preg_match("<!--the loop-->(.*)<!--the loop-->");

for (var i;i<itemCount;i++) {
    $item = items[i];
    // start to replace content - there are many tokens - I have a function
    $newContent = replaceTokensInContent($item, $match);
    $myArray.push($newContent);
}

$newContent = $myArray.join();

// put the content back into the markup 
$updated_markup = preg_replace("<!--the loop-->(.*)<!--the loop-->", $newContent, $markup);

I want to get the contents between both tokens <!--the loop--> and then replace tokens in that multiple times (I have a function) and then put that populated string back into the main string again.

I know how to do this regex in JavaScript but not PHP.

12
  • could you please try put end output you are looking for. Commented Oct 26, 2015 at 6:14
  • 2
    And what is your possible expected output. Regex is not perfect tool to work along with HTML Commented Oct 26, 2015 at 6:14
  • @Uchiha from the leaf clan and Sashant, I just updated the question. I'll add my expected output. Commented Oct 26, 2015 at 6:16
  • 1
    preg_match($needle, $haystack), preg_replace($pattern, $replacement, $haystack). Now check your usages... Commented Oct 26, 2015 at 6:18
  • 1
    Why not use a good parser/DomDocument for this task? Commented Oct 26, 2015 at 6:19

1 Answer 1

1

While you were looking for a way with regular expressions, you might as well consider using a DomDocument approach as marked in the comments.

$doc = new DOMDocument();
$doc->loadHTMLFile(...);

$xpath = new DOMXpath($doc);
$loop = $xpath->query("//*[@id='theLoop']")->item(0);

Having the $loop, you can now insert, delete or replace nodes. Have a look at this explanation as a starting point.

Sign up to request clarification or add additional context in comments.

Comments

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.