0

I am using preg_replace to replace HTML comment tags with empty space but it seems to be replacing the whole HTML comment with empty space.

    echo preg_replace('/<!--(.*?)-->/','',$r->pageCont);

Where $r->pageCont is a database entry containing HTML, for example:

    <div class="col-lg-12">
    <p>The year is:</p>
    <!-- <?php echo date(Y); ?> -->
    </div>

In the above example, the HTML comment tags would be stripped away leaving only the PHP code to echo the year. Like I said, what is happening is the entire HTML comment is being stripped away.

Can someone recommend a pattern to use? Would appreciate your input.

EDIT: updated question to reflect the code I am using.

TimD
  • 271
  • 1
  • 16

1 Answers1

0

It seems like you're trying to replace the comment line with the php code present inside that. If yes, then you need to put the replacement string as $1 so that it would refer to the group index 1.

echo preg_replace('/<!--(.*?)-->/', '$1', $r->pageCont);

DEMO

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
  • Thank you for your help. However, while it did solve my objective by stripping away the HTML comment tags, it did not solve my goal to execute the PHP code within the HTML comments. – TimD Sep 22 '14 at 08:08
  • i think this solevs the current question. If you have any other doubts then ask it as another question. – Avinash Raj Sep 22 '14 at 08:10