Dark Step Dark Step - 10 months ago 38
PHP Question

Regular expression for exchanging variables in a function call

I am upgrading my PHP scripts to use mysqli and it necessitates changing thousands of lines where the variables have exchanged position. For example:

mysql_query($query, $db)

now must become

mysqli_query($db, $query)

Is a regular expression the best way to quickly changes thousands of lines? If so, could someone help me create the regex in order to swap these? I've already changed the function calls for mysqli, but the function parameter swapping is beyond my find/replace skill.

I'm using Visual Studio 2015 find/replace.





mysqli_query($2, $1);

The non-escaped parentheses in the regular expression create capture groups, and $1 and $2 in the replacement pick up what was matched by them.

This will only work for statements like you've posted, where the query is a variable, since \$\w+ just matches a variable name. It won't work if you have literal strings like

mysql_query("SELECT ...", $db);