0

can someone make a regular expression FOR NOTEPAD++ for me to replace this query:

mysql_query($query, $db);

replace by:

$mysqli->query($query)

But $query could also have another variable name, for example $query_2, so the output should be

$mysqli->query($query2)

Thank you!

6
  • 2
    you need an regular expression to change your source code? Don't you need a search & replace on your project? Commented Jul 18, 2013 at 22:19
  • Yes, I need it to change my source code. And that's indeed what I need! Commented Jul 18, 2013 at 22:20
  • I prefer to change them manually! Commented Jul 18, 2013 at 22:21
  • what about a wrapper?? Commented Jul 18, 2013 at 22:22
  • Yes, I want to do that, but my website is very complex with hundreds/thousands of query's in all my scripts, so I would like to create a Regular Expression to replace them. Commented Jul 18, 2013 at 22:22

2 Answers 2

3

Works in Notepad++ 6.3.2. This accommodates any issues with inconsistent space and tab characters potentially being used.

Search Pattern

mysql_query\(\s*\$([^,\s]+)\s*,\s*\$db\s*\);

Replacement

\$mysqli->query\(\$\1\);

(Make sure Regular Expression radio button is selected in the replace dialogue box).

test Text

mysql_query($query, $db);
mysql_query($SQL , $db);
mysql_query($query2,    $db);
mysql_query( $SQL_v4, $db );
mysql_query($AnotherSQL,$db);

Output Text

$mysqli->query($query);
$mysqli->query($SQL);
$mysqli->query($query2);
$mysqli->query($SQL_v4);
$mysqli->query($AnotherSQL);
Sign up to request clarification or add additional context in comments.

Comments

1
$input = 'mysql_query($asdf, $db);';
$input = preg_replace('/mysql_query\(\$(\w+), \$db\);/','\$mysqli->query(\$$1);',$input);
echo $input;

Outputs $mysqli->query($asdf);

EDIT:

On Notepad++, try this:

  1. Find what: mysql_query\(\$(\w+), *\$db\);

  2. Replace with: \$mysqli->query\(\$$1\);

8 Comments

I try to place the regular expression in the Notepad++ search window, but it replaces everything to $mysqli->query($1);
Don't forget to check the "Regular expression" checkbox.
I did, but everything is exactly replaced with \$mysqli->query(\$$1);
It works well for me. What do you mean by "replaces everything"? Is it erasing all your file and replacing it with that?
No, he searches for the mysql_query, but for example mysql_query($query, $db); is replaced by \$mysqli->query\(\$$1);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.