简体   繁体   中英

Get table name from sql string

What is the most efficient way I can get table_name from the following string. All I could think of was substr() but the length of table_name might not always bee the same

SELECT * FROM `table_name` WHERE `id` = '1'

I think this is what you want:

<?php
$query = "SELECT * FROM `table_name` WHERE `id` = '1'";
$pattern = "/SELECT \* FROM `(.*?)`/";
preg_match($pattern, $query, $matches);
print_r($matches);
?>

Then select the correct array element from the $matches array to get the table name. In this case that would be $matches[1]

Try this code, please.

<?php
$query = "SELECT * FROM `table_name` WHERE `id` = '1'";
$pattern = "/SELECT \* FROM `(.*?)`/";
preg_match($pattern, $query, $matches);
echo $matches[1];
?>

Demo

You can do it like below using preg_match() -

<?php

$string = "SELECT * FROM `table_name` WHERE `id` = '1'";

preg_match("/FROM (.*?) WHERE/",$string,$matches);

print_r($matches); // now you can do echo $matches[1];

Output:- https://eval.in/839906 OR https://eval.in/839907

If you want the most efficient, then do not use regex -- it is much slower than non-regex methods. My three methods that follow will all outperform the other answers on this page. The other patterns are not optimized for speed (they use capture groups and don't use negated character classes).

I have ordered my three following methods from fastest to slowest. This is a demo of all three methods .

This is the input for all methods:

$sql="SELECT * FROM `table_name` WHERE `id` = '1'";

All methods will output table_name without backticks.

Method #1 - explode() is fastest, but will only work if there are no backticks before the table name's leading backtick.

// explode(): *only works if no backticks before FROM clause*
echo explode('`',$sql,3)[1];  // limit elements to maximum of 3, we only want the 2nd

Method #2 - strpos() and substr() will be 2nd fastest, and provides 100% reliability because it is locating the backtick that follows FROM (assuming you don't have some zany column name that ends with FROM then a space & you wrapped it in backticks... I mean, you could break it if you tried hard enough.

// pure string functions:
$tick1=strpos($sql,'FROM `')+6;
$tick2=strpos($sql,'`',$tick1);
echo substr($sql,$tick1,$tick2-$tick1);


Method #3 - preg_match() is the slowest of my three methods, but will still be more efficient than all of the other answers. Pattern Demo (9 steps) FYI: Kamrans' = 40 steps, Alive's = 39 steps, Cagy's = 40.

Why is mine so much faster?

  • I am not using parentheses to capture the match, I restart the fullstring match by using \\K .

  • I am also using a negated character class [^ ]+` to match one or more non-backtick characters that follow the first backtick.

You cannot make preg_match() faster than this. As a bonus of using \\K the output array is 50% smaller as well.

// Will work regardless of backticks in SELECT clause:
echo preg_match('/FROM `\K[^`]+/',$sql,$out)?$out[0]:'failed';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM