繁体   English   中英

REGEXP:获取PHP变量的内容,即赋值本身。 具体来说,使用SQL查询搜索变量

[英]REGEXP: get the contents of a PHP variable, the assignation itself. Specifically, searching variables with SQL queries

我想我为此感到疯狂...

我已经尝试了很多组合,但我无法获得好的组合。

使用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变量(特别是字符串),包含与其他变量的部分串联,双引号和单引号字符串,“;”末尾的注释 或在中间...

那我该怎么办?

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

$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令牌生成器-您可以使用它对源进行令牌化(即解析它,以便于理解),然后可以在令牌中查找与您的要求相匹配的字符串和变量,知道每个令牌; 结束一行代码。

不知道这是您要找的东西吗:

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

这将对存储在$结果中的所有任务( 幽会 )。 我用所有样品进行了测试。

对不起,如果您想要其他东西。

说明:

"
\$         # 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
"

暂无
暂无

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

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