phpregexpreg-match

PHP: using REGEX to get the tablename from a mysql query


Consider these three mysql statements:

select * from Users;
select id, title, value from Blogs;
select id, feelURL, feelTitle from Feeds where id = 1; 

Now im not very good at REGEX, but i want to get the table name from the mysql query. Could someone possibly create one for me with a little explanation.

Thanks,


Solution

  • Try:

    preg_match('/\bfrom\b\s*(\w+)/i',$query,$matches)
    

    This will not work if the query has more than 1 table.

    Basically the regex searchs for the complete word FROM in the query and picks the following word as the table name.