Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Regular Expressions to parse functions from a string in PHP

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.

like image 838
JonnyLitt Avatar asked Jul 15 '26 16:07

JonnyLitt


2 Answers

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

like image 66
Gumbo Avatar answered Jul 18 '26 06:07

Gumbo


You could also look into the Reflection class.

like image 20
St. John Johnson Avatar answered Jul 18 '26 07:07

St. John Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!