开发者

Strip ASP code with Preg_Replace

I need to strip legacy ASP code from include files in a PHP app - I'm attempting to match everythi开发者_运维问答ng between <% and %> with the regexp /(<%([.\r\n\r])+%>)/ through preg_replace, but it's failing. Where did I go wrong?


The dot [.] does not apply to all characters when inside a [character class]. Consider this one instead:

/(<%.+?%>)/

It can be read as "match <% then as few of anything as possible, followed by %>". This lazy one won't eat the code inbetween <% ... %> and the next <% ... %>.


To mach everything between <% and %> the expression would be:

/<%(.*?)%>/


You're not using your character class correctly (I assume you're trying to match all charaters and newlines). Try:

/(<%(.)+?%>)/s


preg_replace('/<%(.*)%>/s', '$1', $string);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜