1

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'
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Alex
  • 9,215
  • 8
  • 39
  • 82

4 Answers4

5

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]

Cagy79
  • 1,610
  • 1
  • 19
  • 25
2

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

Kamran Jabbar
  • 858
  • 7
  • 21
2

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

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';
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @Alex Unfortunately you asked your question and awarded your green tick while I was away from my computer. Hopefully it is not too late for you to be impressed with three new, educational methods that will outperform the earlier submissions. If you want efficiency, I've got you covered. – mickmackusa Aug 01 '17 at 01:51
  • Indeed, in my tests your methods all performed slightly faster than the rest. As my criteria was 'efficiency' you deserved the check. Although I do hate being an Indian-giver, I'll have to be less hasty next time when awarding the check. – Alex Aug 01 '17 at 23:20