You cannot do it right with regex. You need to write a parser that can correctly parse comments, string literals and nested brackets.
Regex cannot handle these cases:
class Hello { function foo() { echo '} <- that is not the closing bracket!';
EDIT
Here is a small demonstration of how to use the tokenizer function mentioned by XUE Can:
$source = <<<BLOCK <?php \$mother = new Mother("this function isNotAFunction(\$x=0) {} foo bar"); class Hello { \$foo = 666; public function FunctionName(\$value="username",) { } private \$bar; private function ododeqwdo(\$value='') {
which will print:
<?php $mother = new Mother("this function isNotAFunction($x=0) {} foo bar"); class Hello { $foo = 666; private $bar; } $mother->global(); ?>
which is the original source, only without functions.
source share