这是一个我只想构建参数数组的查询:
$subject = "SELECT * FROM a_table WHERE something='col1=:param1' AND col2=:param2 OR col3=:param3";
我试过了:
$pattern = "^([\"'])(?:(?=(\\\\?))\\2.)*?\\1^";
$subject = preg_replace($pattern, "", $subject);
和
$pattern = '#[:][a-zA-Z0-9_]+#';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
不幸的是,我只获得param2
和param3
作为输出
我希望我的转储看起来像这样:
Array ( [0] => :param1 [1] => :param2 [2] => :param3 )
答案 0 :(得分:2)
您只需使用此正则表达式=(:\w+)
$query = "SELECT * FROM a_table WHERE something='col1=:param1' AND col2=:param2 OR col3=:param3";
preg_match_all('/=(:\w+)/', $query, $params, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($params[1]); $i++) {
echo $params[1][$i]."\n";
}
<强> 样本 强> http://ideone.com/aut6q2