I have some HTML that contains multiple HTML comments, within each comment is a form. I am trying to use preg_replace to replace these comments and the forms within with a tag in the form [CONTACT_FORM_X] where X is the numeric ID of the form.
$str = 'blah blah blah <!-- CONTACT FORM START [CONTACT_FORM_1] -->some form goes here<!-- CONTACT FORM END 1 --> blah blah blah <!-- CONTACT FORM START [CONTACT_FORM_2] -->another form goes here<!-- CONTACT FORM END 2 -->';
$replace = preg_replace('/<!-- CONTACT FORM START \[CONTACT_FORM_\d\] -->.*<!-- CONTACT FORM END \d -->/', '[CONTACT_FORM_X]', $str);
echo $replace;
So:
<!-- CONTACT FORM START [CONTACT_FORM_1] -->some form goes here<!-- CONTACT FORM END 1 -->
Should be replaced entirely with [CONTACT_FORM_1]
And ..
<!-- CONTACT FORM START [CONTACT_FORM_2] --> another form goes here<!-- CONTACT FORM END 2 -->
Should be replaced entirely with [CONTACT_FORM_2]
If I run my code above I get:
blah blah blah [CONTACT_FORM_X]
So my questions are:
How can I get the value of \d and then use this in place of where I currently use X in my preg_replace
My code only seems to replace one of the forms rather than both occurrences. How can I adapt preg_replace to allow multiple replaces