简体   繁体   English

从SQL字符串获取表名

[英]Get table name from sql string

What is the most efficient way I can get table_name from the following string. 从以下字符串获取table_name的最有效方法是什么? All I could think of was substr() but the length of table_name might not always bee the same 我只想到了substr()table_name的长度可能并不总是相同

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. 然后从$ matches数组中选择正确的数组元素以获取表名。 In this case that would be $matches[1] 在这种情况下,将是$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() - 您可以像下面那样使用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 输出:-https: //eval.in/839906https://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. 所有方法将输出table_name而不带反引号。

Method #1 - explode() is fastest, but will only work if there are no backticks before the table name's leading backtick. 方法1 - explode()是最快的,但仅在表名的前导引号之前没有反引号的情况下才有效。

// 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. 方法#2 - strpos()substr()将是第二快的,并且提供100%的可靠性,因为它正在定位FROM后面的反引号(假设您没有一些以FROM结尾的奇特列名,然后是空格,并且您将其包裹在反引号中...我的意思是,如果您尝试足够努力,则可以将其破坏。

// 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. 方法#3 - preg_match()是我这三种方法中最慢的一种,但仍比所有其他答案更有效。 Pattern Demo (9 steps) FYI: Kamrans' = 40 steps, Alive's = 39 steps, Cagy's = 40. 模式演示 (9个步骤)仅供参考:Kamrans'= 40个步骤,Alive's = 39个步骤,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 . 我没有使用括号来捕获匹配项,而是使用\\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. 您不能使preg_match()更快。 As a bonus of using \\K the output array is 50% smaller as well. 使用\\K的好处是输出数组也缩小了50%。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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