Skip to content
Advertisement

Match a PHP class with a Regular Expression

I wanna catch Php classes from a file:

class a {
   function test() { }
}

class b extends a {
   function test() { }
}

and the result matches must be

class a {
   function test() { }
}

and

class b extends a {
   function test() { }
}

Advertisement

Answer

regexps are poor at parsing programming languages’ grammars. Consider tokenizer functions instead. e.g. http://php.net/manual/en/function.token-get-all.php see also this http://framework.zend.com/apidoc/core/Zend_Reflection/Zend_Reflection_File.html

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement