1

How would I with regular expression search for functions which contains the use of a global variable without running "global $var" first?

The files looks like this:

class TestClass
{
    function correctFunc()
    {
        global $var;
        $name = $var->name;
    }

    function invalidFuncIWantToFind()
    {
        $age = $var->user->age;
    }
}

I want to find the function names of all the invalidFuncIWantToFind. At work this would have really speeded up our work but I didn't get how to do it.

Marlun
  • 1,543
  • 3
  • 12
  • 14
  • 2
    Don’t use regular expressions when you’re processing a non-regular language like PHP. – Gumbo Feb 26 '10 at 19:03
  • if you want a quick&dirty&error-prone solution, regexes might work. if you want a stable solution you should look into parser generators for context free grammars (antlr.org for example). what kind of solution do you want? – stmax Feb 26 '10 at 19:12
  • I want a quick and dirty solution. :) I've continued to play with this and "function\s+(\w+).*?(\{.*?(\{.*?\})*.*?\})" seem to work to get all the functions. Now I just need to get those which contains "$var->" without first having "global $var;" Not sure how to do that though. – Marlun Feb 26 '10 at 19:27
  • `$some_var = "{";` breaks your function-extracting regex. You should not use regex for it. Have a look at http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 - same holds for PHP. – viraptor Apr 04 '10 at 02:05

1 Answers1

0

Depending on your Regex implementation, you might be able to use a lookbehind pattern for this. http://www.regular-expressions.info/lookaround.html claims that e.g. the .NET Framework implementation can do this. (Problematic is, that the lookbehind can contain a variable number of letters... this seems to cause trouble). Since I do not have a Visual Studio at hand here, I cannot build a Regex for you, though, sorry.

Jens
  • 25,229
  • 9
  • 75
  • 117