regex – Regular expression to swap variable pair in mysql in PHP file

Question:

Which expression can I use to be able to change the parameters of the MYSQL function, example:

mysql_query($query,$db);

mysql_query($db,$query);

Because I look for mysql_query and change to mysqli_query after I manage to make this change.

That is, swapping two strings that are inside a parentheses.

Answer:

You can invert the arguments by creating three groups at the end, just format the substitution.

The idea is to break this statement as follows, the first group is mysql_query the second $query and the third $db . With the captured elements, reverse the order of the second with the third and add the parentheses.

The first takes the statement: (mysql_query) the second the variable with the query ( \$\w+ ), which is represented by a dollar sign followed by one or more letters, followed by a comma and one more variable.

Search by:

(mysql_query)\((\$\w+)\s*,\s*(\$\w+)\)

Replace with:

\1\(\3,\2\)

Or yet:

$1\($3,$2\)
Scroll to Top