hi I was to get a list of functions declared inside php file using preg_match_all().. example: function foo { while(true) { //do somthing } } function bar{ if(true) { //untrue } else { //false } } other functions etc... PHP: my pattern: "/function ([^}]*)/s" not working.. now I want to get the data of these functions with preg_match_all.. the data should be somthing like: array { [0] => function foo { while(true) { //do somthing } } [1] => function bar{ if(true) { //untrue } else { //false } } } PHP: thanks in advance
$str = "function foo { while(true) { //do somthing } } function bar{ if(true) { //untrue } else { //false } } "; preg_match_all("/function(.*)\{(.*)\}/iUs",$str,&$m); print "<pre>"; print_r($m[0]); exit(); Code (markup): I think it will be helpful .
this worked but not as I expected... I have some comments between functions so this regex failed.. anyway, I just made one my self its not the best solution but it works with "some functions"... so i still have simple problem I don't know how to figure it out yet... here is my pattern till now: $pattern = "/function\s*([^\{]*)\s*\{(.*?)\n\}/s"; my question is.. how can I use the carrot with "\n}" inside the square brackets somthing like [^(\n})] just ignore those 2 characters as a one character? so my final pattern will look like: $pattern = "/function\s*([^\{]*)\s*\{([^(\n})]*)\n\}/s"; the above pattern doesn't work because there is something wrong with the mentioned carrot sign with the square brackets. thanks for your help.
hmmm please show me your $stack and your $needle .... I will perform it's for you . It's mean show me exactly what you want .
here is sample data: $data = "function foo() { while(true) { //do somthing } } /**********************************************aaaaaaaaaaaaa*******************************************************/ function bar() { if(true) { //untrue } else { //false } } /**********************************************adasdaaaaa*******************************************************/ function boo($data){ RETURN 0; } /**********************************************adasdaaaaa*******************************************************/ /********************************************** FUNCTION*******************************************************/ function noo($url, $toot) { if($url) RETURN 1; else RETURN 0; } /********************************************** FUNCTION*******************************************************/"; PHP: I want to extract the functions only... the data should be something like: array{ [0]=> function foo() { while(true) { //do somthing } } [1]=> function bar() { if(true) { //untrue } else { //false } } [2]=> function boo($data){ RETURN 0; } [3]=> function noo($url, $toot) { if($url) RETURN 1; else RETURN 0; } } PHP: hope now you get what I mean thank you in advance..
never mind I fixed the solution... here is the solution: if(preg_match_all("/function\s*([^\{]*)\s*\{(.*?)\n(\s*|)\}/s", $stringgg, $fname)) { print_r($fname); }
actually I still have problem..I couldn't fix it.. it doesn't work with functions like this: function test($toto) { if (true) { if ($toto){ //nothing } else{ //other stuff } } else { //stuff } } PHP:
hmmm ... You need parse all function for what ??? <?php $data = "function foo() { while(true) { //do somthing } } /**********************************************aaaaaaaaaaaaa*******************************************************/ function bar() { if(true) { //untrue } else { //false } } /**********************************************adasdaaaaa*******************************************************/ function boo($data){ RETURN 0; } /**********************************************adasdaaaaa*******************************************************/ /********************************************** FUNCTION*******************************************************/ function noo($url, $toot) { if($url) RETURN 1; else RETURN 0; } /********************************************** FUNCTION*******************************************************/ function test($toto) { if (true) { if ($toto){ //nothing } else{ //other stuff } } else { //stuff } } "; preg_match_all('/function (.*)\((.*)\)(.*){(.*)}/iUs',$data,&$m); print "<xmp>"; print_r($m); exit(); Code (markup):
because I want to remove duplicate functions which I imported from about 100 include files.. I want to get list of all functions inside arrays then use array_unique to remove the duplicates.. edit: it doesn't work with functions like this one: function sample($url, $user) { $url1 = "sample.com"; if(true) { if(false) { $userid = 0; } else if($user) { //nothing } else { //nothing } } else if($test) { $userid = 1; } else if($still) { //delete } else { //exit } $data1 = "dadada"; if($f) { } else if($s) { } return array($a); } PHP:
woohoo I did it again here is my solution. in case if anyone wanted to use it.. $pattern = "/function\s*([^\(]*)\s*\([^\)]*\)\s*\{([^{]*(if|else if|else|while|for)\s*[^\}]*(\}|\;))*\s*.*\s*\}/"; PHP: damn I have been workin on this for 3 days.. for a moment I thought regex has some limitations. but after I found solution for this. I knew its damn powerful thingy to play with.. ps: if you want to add more statements that use the curly braces just add |yourStatement next to (for)..
nah this is not what I needed..this only gives names of functions declared in the script you are currently using.. my pattern brings the whole structures "not only names" of all declared functions in any page you want..