0

Im working on php-html-js templating shell, and i want to callback class, via ajax, as part of template html.

The view of html is something like that:

<center>this is main part</center>
<button class="btns"> class buttons </button>
<div> also main </div>
<!--[part:AJAX]-->
<div> BUT This part at first should be removed with comments, and all inside <!--[part:ANYVAR]--> till
the same comment: </div>
<!--[part:AJAX]-->

But after i need to do an oposite thing : remove all except for inside

Ps thank's for you attention :)

Jacob88
  • 85
  • 12
  • What is your question? What have you tried? – lafor Jul 27 '12 at 12:06
  • can you rephrase the question or give some more details so that question is much clear – swapnilsarwe Jul 27 '12 at 12:14
  • something like INPUT: `THIS IS ` OUTPUT: `value of part:AJAX` – swapnilsarwe Jul 27 '12 at 12:15
  • Hi, thanks for interesting! My question is how to preg_match data only inside my variant of comments, also i need to make an oposite (another time) - remove all inside and self comments (as i show). I was trying to make it by substr, but it not so powerful as regexp.. – Jacob88 Jul 27 '12 at 12:18
  • Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost Jul 27 '12 at 12:46
  • still using
    ? seriously? ;o
    – Adam Wolski Jul 27 '12 at 13:15

1 Answers1

1

This regular expression will find the comments and what's inside:

$pattern = "/(<!--\[part:AJAX\]-->.*<!--\[part:AJAX\]-->)/ms";
preg_match($pattern, $html, $matches);
echo $matches[1];

To remove between comments, change your pattern to:

$pattern = "/(<!--\[part:AJAX\]-->(.*)<!--\[part:AJAX\]-->)/ms";

Then

echo str_replace($matches[2], "", $html);
Tchoupi
  • 14,560
  • 5
  • 37
  • 71