note 38978 deleted from function.create-function by tomsommer

From: Date: Tue, 13 Jan 2004 22:43:40 +0000
Subject: note 38978 deleted from function.create-function by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-63480@lists.php.net to get a copy of this message
Note Submitter: caesar AT dago DOT de ---- The usage of dynamic created functions may be great, but the sample below will get trouble, if there are any blocks ({...}) in the function. I tried to get a way to do this with regex, but i didn't find any suitable. So I'm using the sample below to generate my user functions in a daemonized environment and it works fine. There may be the a problem with '{' and '}' in text because the script is line-oriented, but maybe anybody else will get a solution for this. I thougt of the use of the Tokenizer, but there is no token for the curls. The php_check_syntax() function is new to php5 beta 3 so I recreated it for using it on versions below php5, I think it's working fine so long. <?php if (!function_exists('php_check_syntax')) { function php_check_syntax($file) { $tmp = exec('php -l '.$file); if (preg_match('/No/', $tmp)) return true; else return false; } } $file = 'functions.inc.php'; $in_function = false; $open = 0; $close = 0; $tmp = array( 'name' => '', 'args' => '', 'code' => '' ); if (php_check_syntax($file)) $file = file($file); else die ("ERROR(s) in ".$file.".\n"); for ($i = 0; $i < count($file); $i++) { if (preg_match('/function\ ?([a-zA-Z0-9-_]*)\ ?\((.*?)\)\ ?\{/ix', $file[$i], $matches)) { $in_function = true; $open = substr_count($file[$i], '{'); $close = substr_count($file[$i], '}'); $tmp['name'] = $matches[1]; $tmp['args'] = $matches[2]; $tmp['code'] = '{'; } elseif ($open == $close && $open > 0) { $funcs[] = $tmp; $in_function = false; } else { $tmp['code'] .= $file[$i]."\n"; $open += substr_count($file[$i], '{'); $close += substr_count($file[$i], '}'); } } foreach ($funcs as $val) $$val['name'] = create_function($val['args'], $val['code']); ?> So long, greetZ caesar alias n0pr0b

« previous php.notes (#63480) next »