Stop right there, you don't want to do it this way!
You can use the tokenizer extension of PHP to break a string into tokens, which will reliably find all PHP source code. Then you can do whatever transformation you want with the tokens. Regular expressions are not the tool for this job (You don't want to put out the fire with a spoon, do you?)
$tokens = token_get_all($string);
foreach ($tokens as $token) {
if (is_array($token)) {
if (!in_array($token[0], array(T_INLINE_HTML, T_OPEN_TAG, T_CLOSE_TAG))) {
echo $token[1];
}
} else {
echo $token;
}
}