Regex | swap parameters in a method call | updating old mysql_query with mysqli_query in php 7



 


Swap Params in any function, here is how you do it..


First a little background about Regex, for this propose we are using sublime as a tool

our target is to swap 2 params in a function regardless of the name ans spelling or letters case, as an example we will use the below function call as a case:

mysql_query($deleteSQL, $Data);

as you see above, this is a old function that is un supported by new php version and need to replace it in all code base, not only that we need to rename the function to "mysqli_query" but also we need to swap the params and here is how i did it...


find:

mysql_query[(]([$]\w+), ([$]\w+)[)]

replace:

mysqli_query($2, $1)

 

here is a list of all the find and replace command you need to run if you have a php application made by php 5

find: "mysql_"
replae with: "mysqli_"

find: "mysqli_query[(]([$]\w+), ([$]\w+)[)]"
replace with: "mysqli_query($2, $1)"

find: "mysqli_select_db[(]([$]\w+), ([$]\w+)[)]"
replace with: "mysqli_select_db($2, $1)"

find: "mysqli_query[(]([$]\w+)[)];"
replace with: "mysqli_query($Data,$1);"

find: "<? "
replace with: "<?php "

 

and all the code was updated not only in a single project but also on entire workstation projects all at once .. (:


 


Comments