3

Is there a logical way to strip out function names and their arguments using Regular Expressions in PHP? Currently I have it splitting line by line so that I am able to have each function on each line for easier markup.

So:

doSomeFunction(arg, moreargs, stuff);
breakSomething();
ini_set(include_path, /home/htdocs/);

becomes

array([0] => 'doSomeFunction(arg, moreargs, stuff);', [1] => 'breakSomething();' and so on...

The real problem I am having is getting the function name using regex to have syntax like:

$functionfromregex($args);

The functions are constructed using PHP's "Variable Function Name" ability, so it is possible. Arguments are taken in as arrays since there is no reasonable way to glue them togather the way that PHP needs to parse them as. The real downside is that I am not too familiar with Regular Expressions, any help will be greatly appreciated, and if you need any other information feel free to ask.

1
  • This is one of those cases where you're parsing structured text and a regular expression is not the way to go. Regular expressions aren't a magic wand that you wave at every problem that happens to involve text. Commented Sep 5, 2013 at 12:37

3 Answers 3

5

If you want to parse PHP, you can use token_get_all to get the tokens of the given code.

Sign up to request clarification or add additional context in comments.

1 Comment

That is brilliant and simple. Cheers mate.
1

You could also look into the Reflection class.

Comments

0

So I'm not sure in php, I'm doing something similar in java and the following code strips it in a similar way:

[^(&&[^)&&[^,]]] applied to foo(param1, param2, param3) will return ["foo", "param1", "param2", "param3"].

The problem I'm having is that this will string out all '(' and ')', so there is no way to tell if your param is actually a variable or a method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.