Page 1 of 1

Replace blocks wrapped in HTML comments

Posted: Thu Jan 17, 2008 11:02 pm
by alex.barylski
I have some HTML source like:

Code: Select all

<!--[START]-->This is some HTML content which needs to be replaced dynamically...is it possible?<!--[FINISH]-->
Here is the simple regex I have constructed thus far:

Code: Select all

$regex = '/<!--\[START\]-->(.|\s)+<--\[FINISH\]-->/';
I can keep the regex simple and assume there is NO whitespace between the comments themselves...only the content inbetween START and FINISH may be subject to whitespace or anything else...which is why I'm confused why my regex doesn't work...even remotely. If I remove the proceeding '+' it matches nothing...if I add it...everything goes bye bye. :(

Can someone please help correct my regex so I can continue with my project. :D

p.s-I'm using preg_replace

Cheers :)

Re: Replace blocks wrapped in HTML comments

Posted: Thu Jan 17, 2008 11:46 pm
by Kieran Huggins
try:

Code: Select all

$regex = '/<!--\[START\]-->(.*?)<--\[FINISH\]-->/';

Re: Replace blocks wrapped in HTML comments

Posted: Fri Jan 18, 2008 2:09 am
by VladSun
You need a multiline regexp:

Code: Select all

'/<\!--\[START\]-->(.+)<!--\[FINISH\]-->/s'

Re: Replace blocks wrapped in HTML comments

Posted: Fri Jan 18, 2008 10:24 am
by alex.barylski
Works like a charm...thanks VladSun and Kieran for your efforts. :)