Preg_match从PHP中的SQL获取表名

I'm trying to grab the table names from my sql file for an upgrade script but I have something incorrect.

Lines in my SQL file that have the names are the CREATE TABLE lines:

CREATE TABLE IF NOT EXISTS `ocx_category` (

In the preg_match_all statement I'm using this regex:

preg_match_all('#create\s*table\s*`(\w[\w\d]*)`#i', $sql, $table);

But all $table ends up with:

array (size=2)
  0 => 
    array (size=0)
      empty
  1 => 
    array (size=0)
      empty

Where did I get it wrong?

You can use this regex:

CREATE TABLE.*`(\w+)`

Working demo

By the way, your regex is (\w[\w\d]*) you don't need [\w\d] since \w includes \d. So, basically what I do is to shorten your regex to \w+ and include whatever string you have after "create table"

enter image description here

...table\s*`(\w[...
        ^^^^--incorrect

table followed by zero or more whitespace chars (\s*) followed by a backtick (`). You're not allowing for the "IF NOT EXISTS" portion.