Regular expression to find all table names in a query

前端 未结 12 953
一个人的身影
一个人的身影 2020-12-03 18:09

I am not that hot at regular expressions and it has made my little mind melt some what.

I am trying to find all the tables names in a query. So say I have the query

12条回答
  •  攒了一身酷
    2020-12-03 19:00

    In PHP, I use this function, it returns an array with the table names used in a sql statement:

    function sql_query_get_tables($statement){
        preg_match_all("/(from|into|update|join) [\\'\\´]?([a-zA-Z0-9_-]+)[\\'\\´]?/i",
                $statement, $matches);
        if(!empty($matches)){
            return array_unique($matches[2]);
        }else return array();
    }
    

    Notice that it does not work with a,b joins or schema.tablename naming

    I hope it works for you

提交回复
热议问题