How about:
$test = preg_replace('#(<--)(.+?)(-->)#', '$1Additional line to the html comment without delete the real comment$2$3' , $test);
Вопрос
I was wondering if it's possible to fetch the regular expression match in preg_replace and manipulate it in real time (iow... at the same preg_replace command)...
This is not the proper syntax but just to get the idea...
$test = <<<EOF
<!--
THIS IS COMMENT #1
-->
<!--
THIS IS COMMENT #2
-->
EOF;
$test = preg_replace('#<--(.|\s)*?-->#', 'Additional line to the html comment without delete the real comment' . '#<--(.|\s)*?-->#' .'<BR/>' , $test);
//iow... how can I replace this '#<--(.|\s)*?-->#'
//to this : 'Additional line to the html comment without delete the real comment' . '#<--(.|\s)*?-->#' .'<BR/>'
echo $test;
/*
I expect to get this :
<!--
Additional line to the html comment without delete the real comment
THIS IS COMMENT #1
-->
<!--
Additional line to the html comment without delete the real comment
THIS IS COMMENT #2
-->
*/
Any idea? :/
Решение
How about:
$test = preg_replace('#(<--)(.+?)(-->)#', '$1Additional line to the html comment without delete the real comment$2$3' , $test);
Другие советы
If your comments have always two newlines at the begining, a quick way without regex:
$trans = array("<!--\n\n" => "<!--\n$newline",
"<!--\r\n\r\n" => "<!--\r\n$newline");
$str = strtr($str, $trans);