0

Possible Duplicate:
Parse PHP code to extract function names?

I'm working on a script to read my PHP files and get all the functions as a return.

I'm using preg_split() to split my file into an array of functions.

I'm having trouble writing the pattern to get all the functions returned (faced issue when the function's name contains the word 'function'. I'm open to any other solutions / advice.

Expected output:

array (
  0 => 'function write{$oneparam, $two, $three){return $two}',
  1 => 'function read{$oneparam, $two, $tthree){returne $awesome}',
  2 => 'function edit{$oneparam, $two, $three){return $two},
  3 => 'function delete{$oneparam, $two, $three){return $two}',
  4 => 'function lastfunction{$oneparam, $two, $three){return $two}'
)
Community
  • 1
  • 1

1 Answers1

1

You can try something like this to capture just the names / declarations:

#(function\s+\w+\(.*?\)#s

Or this to include the body of the function (assuming they're on a single line like in your example):

#(function\s+\w+\(.*?\)\s*{.*?})#s

It works for all of your test cases, and without any sample input or more details, I can't elaborate further.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • 3
    If one wanted to capture the complete function body, the `\{.*?}` part would need a nested capture group; possibly a complete syntax representation (only borderline doable) to understand curly braces in "strings". – mario Jun 01 '12 at 23:44
  • "Or this to include the body of the function (assuming they're on a single line like in your example):" is the expected output – user1431754 Jun 01 '12 at 23:46
  • What if they are not on the same line ? (you know regular php) – user1431754 Jun 02 '12 at 00:21