Help needed with regex..

Discussion in 'PHP' started by xenon2010, Apr 15, 2010.

  1. #1
    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 :)
     
    xenon2010, Apr 15, 2010 IP
  2. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    Try it
    
    #function (.*){#Us
    
    PHP:
     
    guardian999, Apr 15, 2010 IP
  3. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    doesnt work... :(
     
    xenon2010, Apr 15, 2010 IP
  4. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    You can try it here http://regexpal.com/
    and this is my regex
    it's should use singleline opton (s)
     
    guardian999, Apr 15, 2010 IP
  5. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    still doesn't give any match...
     
    xenon2010, Apr 17, 2010 IP
  6. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    $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 .
     
    trickyshaun, Apr 17, 2010 IP
  7. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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. :)
     
    xenon2010, Apr 17, 2010 IP
  8. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hmmm please show me your $stack and your $needle :D .... I will perform it's for you . It's mean show me exactly what you want :D .
     
    trickyshaun, Apr 17, 2010 IP
  9. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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..
     
    xenon2010, Apr 17, 2010 IP
  10. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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);
    }
     
    xenon2010, Apr 17, 2010 IP
  11. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    yeah ! Contragulations ! :D :p !
     
    trickyshaun, Apr 17, 2010 IP
  12. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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:
     
    xenon2010, Apr 17, 2010 IP
  13. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    anyone have any idea how to do this?
    regex experts?
     
    xenon2010, Apr 18, 2010 IP
  14. zerophean

    zerophean Peon

    Messages:
    91
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hi.. try this site

    txt2re .com
     
    zerophean, Apr 18, 2010 IP
  15. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    already tried it..
    it doesn't generate proper regex..
     
    xenon2010, Apr 18, 2010 IP
  16. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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):
     
    trickyshaun, Apr 19, 2010 IP
  17. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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:
     
    Last edited: Apr 19, 2010
    xenon2010, Apr 19, 2010 IP
  18. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #18
    woohoo I did it again :p
    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)..
     
    xenon2010, Apr 19, 2010 IP
  19. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Another Solution is use get_defined_functions. lol .
     
    trickyshaun, Apr 19, 2010 IP
  20. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #20
    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..
     
    xenon2010, Apr 19, 2010 IP