Hi, See these 4 strings: 1> function some_name($v1, $a1="3"){ $a3= $v1+$a1; return $a3; } 2> $zzz = 'some value'; 3> $xxx= $yyy; 4> $xxx.= $zzz; (It's php code) Now what I want is an array something like: Array( 'var' => Array( 'v1' => 'leave_blank', 'a1' => 'leave_blank', 'a3' => 'leave_blank', 'xxx' => 'leave_blank', 'zzz' => 'leave_blank', 'yyy' => 'leave_blank' ) 'function' => Array( 'some_name' => 'leave_blank' ) ) Basically, I'm trying to get names of all variables and functions inside a php script. Can someone please help with the regular expressions, or any other method using which I can get these names? Thanks
preg_match_all('/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)/',$code,$matches); $vars = array_unique($matches[1]); preg_match_all('/function[\r\n\t\a\e\f\v\s]+([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)/',$code,$matches2); $functions = array_unique($matches2[1]); PHP: Will give you an array with all variable names and one with all function names, which you can then easily arrange into the format you wanted (or any other format). The main part of the regex is actually given on the PHP Web Site.
Hi, Thanks for that code, I need 1 more help. Now this is my array: Array( 'tkey' => 'value1', 'tk' => 'value2' ); And the strings are: $tkey= 'som1'; $tk= 'some'; I need to replace the $tk, and other with their corresponding values. Like: $value1= 'som1'; $value2= 'some'; I'm not able to get the preg_replace work correctly. The script never finishes. Sometimes when it worked, I got: $value2ey= 'som1'; (this should get replaced with $value1) $value2= 'some'; which is all wrong... I needed the variables to be replaced something like: $ sign just before "array key", and no [A-Za-z0-9] after the "array key". The array key gets replaced with it's value. How to do this? Thanks
I don't completely understand what you're trying to do, from the sounds of it your problem is due to the varname you're trying to replace being in other variable names and you're looking for something like: $newcode = preg_replace('/\$'.$varnametoreplace.'([^a-zA-Z0-9_\x7f-\xff])/','$'.$replacementvalue.'$1',$code); PHP:
Hello, This is similar to what I want, but it did not replace anything. I wanted to replace the "variable" with it's value in the array. For example if this is the code: $tvar= 'some'; $tv= 'more'; Then it gets replaced something like: $value_tvar= 'some'; $value_tv= 'more'; if the array is like this: 'tvar' => 'value_tvar', 'tv' => 'value_tv' so $tv will become $value_tv Thanks
Hi, It worked: $c= preg_replace('/\$'. $k. '([^a-zA-Z0-9_\x7f-\xff]*)/','\$_'.$kk.'$1',$c); Although for some reason the script is now giving a 500 error... I was trying to use pobs.php to scramble my code, but there's a bug in it which is breaking the code, so thought of making a new script to do this. Not working yet... The bug in pobs is that it's also changing the querystring like: ?u='. $myvar. '&t=something'; Pobs does ?VVVJJJDH='. $VVVhhggdghud. '&Vaaslkjkdj=something'; The ?u= part is also changed, which breaks the script completely... Anyway, Many thanks for this. Green reputation added. Thanks
Although having that * in the regex doesn't affect the output, it's not needed and causes unnecessary overhead.