Page 1 of 1

[SOLVED] Parsing PHP Source Code

Posted: Tue Mar 30, 2004 12:26 pm
by EvilWalrus
OK, got halfway through my reloading modules problem... now, i need a regex that can parse a module file for functions...

The module file is just a file, with PHP functions in it... what i need to do is, grabs the contents of that file, parse for all instances of a a function definition, and parse out each of the function's important parts... function name, arguments, and main function code.

If anyone has a useful regex anywhere,or can make one, i'd sure appreciate it... i had one whipped up myself, but it doesn;t want to work....

Code: Select all

/function\s?(їa-zA-Z0-9-_]*)\s?\((ї^{]*?)\)\s?\{(.*)\s?(ї^}]*?)/mixse
Also, here's the code that needs to be parsed:

Code: Select all

<?php

function foo($obj, $arg)
{
     $data = $arg
     return $data;
}

function bar($obj, $arg2)
{
     $data = $arg2
     return $data;
}

?>
The regex needs to literally parse out the function's name, arguments, and internal code... for each function in the file. Thanks in advance.

Posted: Tue Mar 30, 2004 2:15 pm
by redmonkey
Not sure exactly what you are trying to do and exactly how you want the info split but the following my give you some ideas....

Code: Select all

$string = <<<EOF
function foo(\$obj, \$arg)
{
     \$data = \$arg
     return \$data;
}

function bar(\$obj, \$arg2)
{
     \$data = \$arg2
     return \$data;
}
EOF;


if (preg_match_all('/^function\s?([a-z0-9_]+)\s?\(([^\)]*?)\).*?{(.*?)^}/ism', $string, $matches, PREG_SET_ORDER))
{
	foreach ($matches as $function)
	{
		$func_name = $function[1];
		$func_args = array_map('trim', explode(',', $function[2]));
		$func_code = $function[3];
		
		echo "Function Name : $func_name\n";
		foreach ($func_args as $argument)
		{
			echo "Function Argument : $argument\n";
		}
		echo "Function Code : $func_code\n";
	}
}
Outputs....

Code: Select all

Function Name : foo
Function Argument : $obj
Function Argument : $arg
Function Code : 
     $data = $arg
     return $data;

Function Name : bar
Function Argument : $obj
Function Argument : $arg2
Function Code : 
     $data = $arg2
     return $data;

Posted: Tue Mar 30, 2004 4:55 pm
by kettle_drum
What i do to deal with modules is to put them all into a class and call it after your module : evilw_module_blah

Then in the class constructor you can simply call get_class_methods() to return all the methods in the class and then pass that back to where you want that data.

Or you could create your own array in the class that holds hard coded data of what functions exist and what vars they take, this way you can control what functions can be used - as you only pass back what you want people to use.

Posted: Tue Mar 30, 2004 9:31 pm
by EvilWalrus
I've solved my own problem... managed to use the tokenizer extension to get things sorted. thanks.