Can anyone help me with regex problem. Im making a script to go through all my .php files and get all strings passed to certain function. I need to match this cases:
/* Double quotes */
function("some string"); // Match: some string
function("some \"string\""); // Match: some "string"
function("some 'string'"); // Match: some 'string'
/* Single quotes */
function('some string'); // Match: some string
function('some \'string\''); // Match: some 'string'
function('some "string"'); // Match: some "string"
Function can also accept parameters after string, so it also needs to match this cases:
/* Additional parameters */
function("some string", "param"); // Match: some string
function("some string", $param); // Match: some string
So essentially, param can be either a string (quoted or double quoted) or unquoted variable.
I need to get string only from first parameter of function, regardless if second parameter exists or is quoted in any way…
Thanks in advance…
Your particular example was successfully processed with…
Here’s ideone demo.
Explanation: we try to match opening parenthesis (thankfully, it’s PHP; it’d be far more difficult in Ruby), followed by any number of whitespace characters, followed by…
"(\\.|[^"])+"'(\\.|[^'])+'… followed by optional comma.
Each of this sequences covers both ‘special characters’ (escaped with slash) and ‘normal ones’ (that are not the same as delimiters).