【问题标题】:REGEXP: get the contents of a PHP variable, the assignation itself. Specifically, searching variables with SQL queriesREGEXP:获取 PHP 变量的内容,赋值本身。具体来说,使用 SQL 查询搜索变量
【发布时间】:2011-12-21 00:05:28
【问题描述】:

我觉得我要疯了……

我尝试了很多组合,但我无法找到好的组合。

在使用file_get_contents() 阅读后,我需要在 PHP 代码中找到所有 SQL 查询。

当然,所有这些查询都是变量赋值,例如:

$sql1 = "
  SELECT *
  FROM users u
  WHERE u.name LIKE '%".$name."%' AND ... ;
";

$sql2 = "
  SELECT *
  FROM users u
  WHERE u.id = ".$user_id;

$sql3 = '
  SELECT *
  FROM users u
  ORDER BY u.surname1 DESC
'; //this query blablabla.......

因此您可以看到 PHP 变量需要考虑的因素很多。

首先,我尝试了基于获取变量本身和获取它的内容的近似值...

我也尝试在正则表达式模式中从 SQL 中查找特定单词...

随便...

我不知道该怎么做。

  1. 获取所有变量及其赋值,对赋值进行分组,然后在它之后循环匹配搜索特殊 SQL 词(这就是我现在所拥有的,但它不起作用导致赋值正则表达式部分)。

  2. 直接搜索具有良好正则表达式的 SQL 查询?

PHP 变量(特别是字符串),包含与其他变量的部分连接,双引号和单引号字符串,“;”末尾的 cmets还是在中间……

那我该怎么办?

到目前为止,这是我的变量正则表达式部分:

$regex_variable = '\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*[\+\-\*\/\%\.\&\|\^\<\>]*=\s*';

我将它与我尝试过不同形式的 $regex_sql 连接起来:

//$regex_sql = '(["\'])(.*?)\2\s*;';
//$regex_sql = '(["\'])([^;]*?)\2\s*;';
//$regex_sql = '(?<!")\b\w+\b|(?<=")\b[^"]+';
//$regex_sql = '([^;]+)(?<=["\']);(?!["\'])';
//$regex_sql = '(.*?;)[^\\$]*';

这些都不能正常工作。

你能帮帮我吗?我确信最好的近似值是获取所有变量本身,然后测试分配是否包含一些特殊的 SQL 单词,如 SELECT、WHERE、UNION、ORDER、...

提前非常感谢!

标记。

编辑:

当然,要补充一点,带有查询的变量可以有任何形式。以上只是简单的例子。

我们谈论的是这样的事情:

$s = 'insert into tabletest(a,b,c) values('asd','r32r32','fdfdf')';

$where = 'where a=2';
$sql="select distinct * from test ".$where;

$a = '
select *
from users
left outer join ...
inner join ...
left join ...
where ...
group by ...
having ...
order by ...
limit ...
...
';

...

想象一下很多程序员,在代码中创建查询,每个人都以自己的方式做...:\

我必须得到所有这些。至少,最大化结果... ^^'

【问题讨论】:

    标签: php regex variables variable-assignment


    【解决方案1】:

    我建议您查看PHP Tokenizer - 您可以使用它来标记您的源(即解析它以便更容易理解)然后您可以查看符合您要求的字符串和变量的标记,知道每个令牌; 结束一行代码。

    【讨论】:

      【解决方案2】:

      不知道这是不是你要找的:

      preg_match_all('/\$.*?=(.*?)(?<=[\'"]);/s', $subject, $result, PREG_PATTERN_ORDER);
      $result = $result[1];
      

      这会将所有分配(assignations)存储在 $result 中。我用你所有的样品测试了它。

      对不起,如果你想要别的东西。

      解释:

      "
      \$         # Match the character “\$” literally
      .          # Match any single character
         *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
      =          # Match the character “=” literally
      (          # Match the regular expression below and capture its match into backreference number 1
         .          # Match any single character
            *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
      )
      (?<=       # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
         ['\"]       # Match a single character present in the list “'\"”
      )
      ;          # Match the character “;” literally
      "
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-28
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 2017-11-29
        • 1970-01-01
        • 2011-01-07
        相关资源
        最近更新 更多