preg_match function call

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

preg_match function call

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

hm...oksi..this sounds pretty good. thanks

Still, do you think it is possible to catche function call with regexp?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No, and tokenizer alone doesn't either. That's why I deleted my last post. sorry.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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
Post Reply