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.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • could you please try put end output you are looking for. – Sashant Pardeshi Oct 26 '15 at 06:14
  • 2
    And what is your possible expected output. [Regex is not perfect tool to work along with HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Narendrasingh Sisodia Oct 26 '15 at 06:14
  • @Uchiha from the leaf clan and Sashant, I just updated the question. I'll add my expected output. – 1.21 gigawatts Oct 26 '15 at 06:16
  • Use a XML/HTML parser instead of regex. – hjpotter92 Oct 26 '15 at 06:16
  • 1
    `preg_match($needle, $haystack)`, `preg_replace($pattern, $replacement, $haystack)`. Now check your usages... – Justinas Oct 26 '15 at 06:18
  • 1
    Why not use a good [parser/DomDocument](http://php.net/manual/en/class.domdocument.php) for this task? – Jan Oct 26 '15 at 06:19
  • Why not simply to use existing templating engine like Handlebars (much closer to your html) or Jade (More powerful and best readability), for example? – bitifet Oct 26 '15 at 06:35
  • @Jan from your link it says that the parser adds the "html" and "body" tags if your code doesn't have them. So I thought, no problem, I'll just use RegEx to remove them. Then I thought, that's more work than just getting this code working. – 1.21 gigawatts Oct 26 '15 at 07:17
  • @Uchiha I read your link "RegEx is not the perfect tool..." and I get that someone had problems with it. I'm using as string manipulation, not HTML parser. It's looking for two tags and replacing content, exactly what it was created for. I also control the parts so I know what I'm working with. – 1.21 gigawatts Oct 26 '15 at 07:19
  • @Jan do those parsers you recommend provide errors? for example, if I give it my HTML value and it's gibberish, will it give any errors or "make a best attempt" and fail silently? fyi, i want error messages. – 1.21 gigawatts Oct 26 '15 at 16:02
  • @1.21gigawatts: Well, the underlying internal libraries provide some errors but not really, no. See here on SO for more information on [error handling with DomDocument](http://stackoverflow.com/questions/1759069/php-domdocument-error-handling). – Jan Oct 26 '15 at 17:03
  • Ugh, this is the half the problem with web development... no error messages, no warnings, nothing. The fact there's not even an option is ... upsetting. – 1.21 gigawatts Oct 26 '15 at 17:32

1 Answers1

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.

Community
  • 1
  • 1
Jan
  • 42,290
  • 8
  • 54
  • 79