开发者

Strip ASP code with Preg_Replace

开发者 https://www.devze.com 2023-03-15 13:24 出处:网络
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_rep

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

精彩评论

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