I'm trying to parse a function in php to 3 things: name, arguments and content.
I have a regex that do that good, but the problem is the he can parse only one function. i.e - if i wrote a function he can parse it, but if i wrote a function and a print_r he will mixed up.
The regex is this:
preg_match("/function.+?([a-zA-Z0-9_]+?)[\s]*\((.*?)\)[\s]*\{(.+)\}/is", $code, $functions);
I didn't wrote it, someone helped me becuase i'm suck in regex. Thank you for help.
Best regards, Roy.
I'm not sure i understand your problem, but it seems to be that your regex won't parse multiple functions from the $code variable?
Here is a slightly re-written regexp that can handle multiple functions
$code = 'function myfunc1() { echo "This is myfunc1"; } function myfunc2($arg1 = array(), $arg2) { echo "This is myfunc2"; print_r($arg1); }';
preg_match_all("/function\s*([a-zA-Z0-9_]+)\s*\((.*)\)\s*\{(.*)\}/Uis", $code, $functions);
echo '<pre>';
print_r($functions);
echo '</pre>';
And the output
Array
(
[0] => Array
(
[0] => function myfunc1() { echo "This is myfunc1"; }
[1] => function myfunc2($arg1 = array(), $arg2) { echo "This is myfunc2"; print_r($arg1); }
)
[1] => Array
(
[0] => myfunc1
[1] => myfunc2
)
[2] => Array
(
[0] =>
[1] => $arg1 = array(), $arg2
)
[3] => Array
(
[0] => echo "This is myfunc1";
[1] => echo "This is myfunc2"; print_r($arg1);
)
)
This will however run into problems if your function bodies contain the { } characters. I don't think that can be handled by a single regular expression.