A little note on finding those tags – they don’t necessarily need a whitespace following them. [code][/code] works too, ugly as it may seem. Use a regex with a negative lookahead to be certain to find them all, and be sure to include XML as an exlusion: [code]/<\?(?!php|=|xml)/[/code]

As for replacing them automatically, it's a bit trickier. Perhaps obviously, [code][/code] will throw a parse error, so you’ll want to throw a space character in the end of that replacement string IF it’s not already followed by a whitespace (wouldn’t want any of those silly old trailing spaces or double spaces, would we now). Probably there’s a clever regex to do all that in one go, but the easier thing to do might be to just make two passes of it:

[code]/ “<?php "
/ “<?php"[/code]

Finally… and my condolences to folks with large legacy codebases, but it should be said: review the replacements manually. There may very well be exceptions that shouldn't be replaced. I know Smarty used to try and match opening PHP tags (including short tags) in a lexer for whatever reason, although I can't find that in the current version. Maybe there's some obscure sanitisation function that checks for PHP tags. I've found several exceptions in that vein in legacy codebases when refactoring them, so just be on your guard for that sort of thing. If you use PhpStorm to refactor, keep an extra eye on anything on the section "Usage in string constants".