- preg_replace: documentation ( source)
<?php
$str = '1st blah blah <!-- CONTACT FORM START [CONTACT_FORM_1] -->some form goes here<!-- CONTACT FORM END 1 --> 2nd blah blah <!-- CONTACT FORM START [CONTACT_FORM_2] -->another form goes here<!-- CONTACT FORM END 2 -->';
$pattern = '/<!-- CONTACT FORM START \[CONTACT_FORM_(\d+)\] -->.*<!-- CONTACT FORM END \1 -->/';
$replace = preg_replace($pattern, '[CONTACT_FORM_$1]', $str);
echo $replace;