我正在尝试查找引号外的参数(单或双)
$sql = "
INSERT INTO Notifify (to_email, msg, date_log, from_email, ip_from)
VALUES
(
:to_email,
'test teste nonono',
'2013-02-01 10:48:27',
'bar@foo',
:ip_from
)
";
$matches = array();
preg_match_all('/:[A-Za-z0-9_]*/', $sql, $matches);
上面的代码会产生如下结果,
print_r($matches);//数组(:to_email, :48, :27, :ip_from)
我只想要:
:to_email
:ip_from
最佳答案
'/^\\s*:[A-Za-z0-9_]*/m'
应该这样做,检查行首和空格,并确保将 RegEx 查询设置为多行。
编辑
preg_match_all('/(?:^\\s*)(:[A-Za-z0-9_]+)/m', $sql, $matches);
print_r($matches[1]);
这使用被动非捕获组 (?:) 将正确的结果放入索引 1 处的 matches 变量的子数组中,没有空格填充。
关于php - 正则表达式查找不在引号中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14647560/