I’m working on a script for looping thru PHP files and renaming function calls when necessary. This selection needs to be done by the following rules:
- NO function declaration //function test() {
- NO static functions //SomeClass::staticMethod()
- NO class functions // $class->method()
- ONLY function calls // myFunction()
Currently I have the following regex:
preg_replace_callback("/((?<=function\s|\:\:|->)[a-z][a-zA-Z0-9\_]+)\(.*\)/i", 'test', $content);
- Function names are mostly written in camelBack notation.
- Sometimes function names contains a underscore.
This is a positive look behind assertion: it gives all the given rules classified by NO 🙂
So I have to change it in the negative way but that is my problem. When I try, it’s not working as proposed.
EDIT
When i try a negative look behind assertion
'?<!' instead of '?<='
With the example above it gives the following output:
- est() (missing first)
- taticMethod() (missing first)
- ethod() (missing first)
- myFunction() (correct)
It drops the first letter of the functions I don’t wanna match. Is there anyone who knows how to do the trick?
You seem to have misplaced a parenthesis. Your current regex seems to drop the first character for camelcase functions. This seems to work better (tested with a
preg_match_all):On a side note, you’re use of
preg_replace_callbackis incorrect, since it’s 2nd argument should be acallableEDIT
As per PO’s comment, my first try was dumb 🙂
The main problem with your expression is that if it starts with
::it will drop out this part, but the first letter of the method name will match your first subexpression and the rest of it will match as a method name.I tried another approch with character groups, let me know how it works out for you: