Page 1 of 1

preg_match function call

Posted: Tue Oct 24, 2006 8:57 am
by jmut
Hi,
I want to catch all function calls like....
file()
fopen() or whatever

Anyone dare to give a prage match for this one. Mind that it could be $arr['file'] or something tricky. I want to catch function calls only.

I have come to this till now ...but it kind of fails :)
So maybe do not look at it at all.

Code: Select all

$preg = '%\b(file)\b(?![\'\"]\])\s*[\(\'\"].*\$.*[\)\'\"]%';

Another better way would be an PHP BNF parser..if anyone knows about such stable thing.


Thanks a lot for time spend.

edit: just noticed this is better for regexp board. sorry

Posted: Tue Oct 24, 2006 9:01 am
by jmut
hm...oksi..this sounds pretty good. thanks

Still, do you think it is possible to catche function call with regexp?

Posted: Tue Oct 24, 2006 9:04 am
by volka
No, and tokenizer alone doesn't either. That's why I deleted my last post. sorry.

Posted: Tue Oct 24, 2006 9:33 am
by Mordred
Hmm, yeah, how come the tokenizer doesn't recognize function calls?

Still, the tokeniser is the way to go, regexes cannot do it (don't ask me about the theory behind it :)

Code: Select all

print_r(token_get_all("<?function a(){} a();?>"));

Code: Select all

Array
(
    [0] => <
    [1] => ?
    [2] => Array
        (
            [0] => 332
            [1] => function
        )

    [3] => Array
        (
            [0] => 357
            [1] =>  
        )

    [4] => Array
        (
            [0] => 305
            [1] => a
        )

    [5] => (
    [6] => )
    [7] => {
    [8] => }
    [9] => Array
        (
            [0] => 357
            [1] =>  
        )

    [10] => Array
        (
            [0] => 305
            [1] => a
        )

    [11] => (
    [12] => )
    [13] => ;
    [14] => Array
        (
            [0] => 356
            [1] => ?>
        )

)
305 = T_STRING

Maybe you can look for a T_STRING, followed by some whitespaces and then a '('

This has no chance of catching the alternative ways of calling functions - by strings, eval-s, callbacks