PHP: How do I find (Oracle) parameters in a SQL query?
Suppose you have a string: "SELECT * FROM TABLE WHERE column1 = :var1 AND column2 = :var2"
Now, how do I get an array with all the variables like so:
Array
(
[0] => :var1
[1] => :var2
)
I've trie开发者_StackOverflow中文版d it with PHP's preg_match_all, but I struggle with the regex.
$varcount = preg_match_all("/ :.+ /", $sql, $out);
Try this regex instead:
/:\w+/
\w
matches any of a-z
, A-Z
, 0-9
, or _
, which is generally what parameter names consist of.
So for your code,
$varcount = preg_match_all("/:\\w+/", $sql, $out);
精彩评论