Re: Regex guru needed
| From: | George Schlossnagle | Date: | Sat, 13 Nov 2004 17:28:08 +0000 |
| Subject: | Re: Regex guru needed | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-34377@lists.php.net to get a copy of this message | ||
On Nov 13, 2004, at 9:44 AM, Tobias Schlitt wrote:
Hi all! I try creating a regex which extracts all function/method prototypes from a PHP file. So far, I went pretty well, besides one problem. The regex below works pretty well, but only the last parameter of a function declaration is extracted. Maybe someone can give me a hint how to get all parameters (into seperate array elements)?Hard to do the whole match with a single regex, I think. Here's something that works for on my test case: <?php $data = file_get_contents($argv[1]); $prot_re = '/function\s+(\w+)\s*\((.*?)\)/ims'; $arg_re = '/(?:(\w+)\s+)?(\$\w+)(?:\s*=\s*(\w+))?/'; preg_match_all($prot_re, $data, $m, PREG_SET_ORDER); foreach($m as $func_m) {
$args = array();
if(preg_match('/\S/', $func_m[2])) {
$args_t = array_map('trim', split(',', $func_m[2]));
foreach($args_t as $arg) {
preg_match($arg_re, $arg, $arg_m);
$args[] = array ('typehint' => $arg_m[1],
'name' => $arg_m[2],
'default' => @$arg_m[3]);
}
}
$functions[]= array( 'name' => $func_m[1], 'args' => $args);
}
print_r($functions);
?>
George