1

I have a simple problem. I have a PHP file which contains a lot of function-calls like this one:

return self::_Call('client_changePassword',Array(
    $token,
    $password,
    $user_token
));

The name and the number of parameters can vary but the call itself always have this structure, including spaces and linebreaks.

What I need is a regular expression that will grab all these calls from the PHP file and split them into the function name (client_changePassword) and a group of parameters. Since the file has several hundreds of different calls, it needs to be reasonably fast, too.

The regular expression would be used within C# as part of a console application that will generate a simple code analysis. Basically, I need a list of methods plus parameters that PHP calls in the source and compare it with a list of definitions that is stored in a different project. This would help our code reviews a lot.

No, this is not PHP-related even though I'm trying to parse PHP code. My system can't run any PHP code and I don't even want to run PHP code.

1
  • WTH? you marked the accepted answer that contains a PHP solution and you still insist on C#? Commented Jul 8, 2015 at 10:37

1 Answer 1

1

You can use the following regex:

return\s+self::_Call\('(?<name>[^']+)'\s*,\s*[Aa]rray\((?:\s+\$(?<param>[^,)\n]+),?\r?\n)+

See demo

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! Added a few modifications to create this one, which is slightly better: return\s+self::_Call\('(?<name>[^']+)'\s*,\s*[Aa]rray\((?:\s+\$(?<param>[^,^)^\r^\n]+),?\r?\n)+
Yes, named captures are way better and more readable. However, [^,^)^\‌​r^\n] is a bit clumsy, I'd use [^,)\‌​r\n] (or [^,)\n] since regexstorm complains of \r\n inside the character class).
[^,)\‌​r\n] works fine for me, so I've changed it into that. I still need to check for both \r and \n since there's no guarantee in which order they will appear in the PHP file.
It might be even unnecessary if the file was created in Linux/Unix since linebreaks are just \n there.
The PHP file is either generated on a Mac or Windows, depending on the developer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.