Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace via macro in Sublime Text 2 to switch from mysql_ to mysqli_

I have a bunch of code that needs transitioned from PHP's mysql_* to mysqli_*

In Sublime Text 2 I can easily do a find for mysql_ and replace it with mysqli_

The problem I run into is:

mysql_query($query, $link_identifier)

versus

mysqli_query($link, $query)

To make things worse $query is not always the variable name for the query string. I may have:

$q_test = "SELECT `lName` FROM `users` ORDER BY `lName`";
$rstest = mysql_query($q_test, $DB) or die(mysql_error($DB));

and

$q_tester = "SELECT `address` FROM `users_address` ORDER BY `id`";
$rstester = mysql_query($q_tester, $DB) or die(mysql_error($DB));

on the same page.

Is there a way for me to construct a macro in Sublime Text 2 to change both of the above to:

$rstest = mysqli_query($DBi, $q_test) or die(mysqli_error($DBi));
$rstester = mysqli_query($DBi, $q_tester) or die(mysqli_error($DBi));

at the same time? I'm thinking I can use a regex to accomplish this I just have no idea how and where to start.

UPDATE

So, after a little hunting I found that I can search for:

mysql_query\(\$(\w+), \$DB\)

and replace with

mysqli_query\(\$DBi\, \$$1)

which converts $rstest = mysql_query($q_test, $DB) or die(mysql_error($DB));

to

$rstest = mysqli_query($DBi, $q_test) or die(mysql_error($DB)); for me but...

turns out you can't macro Find and Replace. Do I have any options?

UPDATE

So it looks like I can use the plugin Reg Replace to accomplish what I want even though I have more setup to do.

Using Reg Replace I can chain multiple find and replace actions together and bind them to a keystroke so I can basically do a series of find and replace actions to get what I need:

  1. Find and Replace mysql_ with mysqli_
  2. Find and Replace $DB with $DBi
  3. Find and Replace , $DBi) or die( with ) or die(
  4. Find and Replace mysqli_query($ with mysqli_query($DBi, $

Those basically get

$rstest = mysql_query($q_test, $DB) or die(mysql_error($DB));

converted to

$rstest = mysqli_query($DBi, $q_test) or die(mysqli_error($DBi));

for me in a single keystroke. It's not optimal but it does the trick. I'm still looking for a simple, no plugin solution however.

UPDATE The above works with Sublime Text 3 using the updated Reg Replace plugin

like image 431
Jason Avatar asked Oct 16 '12 11:10

Jason


1 Answers

Since sublime does not have the ability to record find and replace using a regex in a macro for now I'm going to use Reg Replace and a series of chained find and replace commands to get what I'm looking for.

like image 90
Jason Avatar answered Sep 18 '22 13:09

Jason