0

Possible Duplicate:
A PHP regex to extract php functions from code files

I have a file with a list of functions formatted something like this:

function foo(){
      ...code { code{}  } code...
}

For this particular file I will always place the word 'function' all the way to the left and the ending curly brace all the way to the left. As is customary, the code within the function will always be indented. The code within the function may contain any character including curly braces.

I would like to parse the file with PHP to get an associative array of functions where the names of the functions are the keys. I just started with this, here is a very simple start:

$regex = "/function.*/";
preg_match_all($regex, $str, $result, PREG_PATTERN_ORDER);
$arr = $result[0];

print_r($arr);

This produces the following and stops at each new line:

Array
(
    [0] => function foo(){
    [1] => function bar(){
    [2] => function stop(){
    [3] => function go(){
)

I tried changing the regex to:

$regex = "/function.*\n}$/s";

My thinking was that where there is a new-line character directly follwed by a ending-curly brace, \n}$ would match the end of the function. However, this does not work, it produces an array with one long element that contains everything after function foo()

I have not started to get the function name into the key of the associative array.

0

2 Answers 2

2

@John R

This is the regex solution:

$regex = '~
  function                 #function keyword
  \s+                      #any number of whitespaces 
  (?P<function_name>.*?)   #function name itself
  \s*                      #optional white spaces
  (?P<parameters>\(.*?\))  #function parameters
  \s*                      #optional white spaces
  (?P<body>\{.*?\})        #body of a function
~six';

if (preg_match_all($regex, $input, $matches)) {
  print_r($matches);
}
Sign up to request clarification or add additional context in comments.

Comments

0

An expression like this could be sufficient in your case:

/^function\s++(\w++).*?^}/ms

It stops the match at the first } that is not indented. Function name will be in the first capturing group, and the whole match is the function.

4 Comments

I will study your solution, but I am getting an empty array by changing to $regex = "/^function\b\s+(\w++).*?$}/ms";
@Qtax: Please consider to add your answer to the duplicated question instead.
@hakre, can't because it's not the same question. In this question the indentation is specified (and enables such a simple expression), which it's not in the other question.
@JohnR, replace $ with ^ and use single quotes, not double quotes, or you need to double escape the backslashes. (You can always print/echo the variable to make sure you got the correct value.) $re = '/^function\b\s+(\w++).*?^}/ms';.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.