views:

18

answers:

1

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 tried it with PHP's preg_match_all, but I struggle with the regex.

$varcount = preg_match_all("/ :.+ /", $sql, $out);
+1  A: 

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);
Amber
Ah, thanks. That works great!
Bartb