douhensheng1131 2014-08-26 20:44
浏览 35
已采纳

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?

  • 写回答

2条回答 默认 最新

  • drl971115 2014-08-26 20:46
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办