PHP替换反引号中的点

I have a project where I need to modify the table names of a lot tables in my database and therefore a lot of SQL queries.

Right now my tables all have names like 1.customers, 2.customers, etc I need to update these to 1_customer, 2_customers, etc.

Is it possible, through preg_replace to replace all dots within backticks with an underscore

For example I would want

"UPDATE `1.customers` SET `value` = '1.0'"

to become

"UPDATE `1_customers` SET `value` = '1.0'"

There are approximately 50 tables in all.

Use capture groups in the regular expression to match the parts before and after the dot, so you can copy them into the replacement.

$str = preg_replace_callback('/(`\d+)\.(\w+`)/', '$1_$2', $str);

This assumes there's just one dot in each name. If not, you can use preg_replace_callback() to match a name with any number of dots, and then use str_replace() in the callback function to replace all the dots with underscores.

Replaces all dots within backticks with an underscore

$input = "UPDATE `1.customers` SET `value` = '1.0'";

$output = preg_replace_callback(
  '/`(.+?)`/',
  function ($matches) {
    return str_replace('.', '_', $matches[0]);
  },
  $input
);

$output

UPDATE `1_customers` SET `value` = '1.0'