我想我要疯了.
我已经尝试了很多组合,但我无法得到一个好的。
我需要在用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中找到特定的单词.
不管什么..。
我不知道怎么做。
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 ...
...
';或
..。
想象一下很多程序员,在代码中创建查询,每个人都以自己的方式.*\
我得把他们都找来。至少,把结果最大化..。^^‘
发布于 2011-11-03 09:32:32
我建议您查看一下PHP托卡器 --您可以使用它来标记您的源代码(即解析它,以便更容易理解),然后您可以通过令牌查找与您的需求相匹配的字符串和变量,同时知道每个令牌;都结束了一行代码。
发布于 2011-11-03 09:35:54
不知道这是不是你要找的:
preg_match_all('/\$.*?=(.*?)(?<=[\'"]);/s', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[1];这将将所有赋值(分配)存储在$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
"https://stackoverflow.com/questions/7992455
复制相似问题