[SOLVED] Parsing PHP Source Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

[SOLVED] Parsing PHP Source Code

Post 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.
Last edited by EvilWalrus on Tue Mar 30, 2004 9:32 pm, edited 1 time in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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;
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post by EvilWalrus »

I've solved my own problem... managed to use the tokenizer extension to get things sorted. thanks.
Post Reply