I'm trying to validate a query with preg_match. I want to replace all %s, %d and %f but NOT with \%s, \%d and \%f. My regex is:
$query = "UPDATE FROM lol SET lol = \%s, asd = %s WHERE lol = %d";
$reg = preg_match("(\%s|\%d|\%f)(?!\\\%s|\\\%d|\\\%f)", $query, $matches);
var_dump($matches); // %s and %d, because \%s can't be matched
I'm trying to not match with \%s, because if you do something like: "WHERE st LIKE %s" (starting with s) it will crash. Then I want to validate it with a regex and after replacing to remove the slashes. I will use the matches %s, %d and %f to replace it like str, int and float, so I want to use just one regex. Can you help me?
Zerquix, here's the most compact regex I could think of. It only matches the right ones.
$regex = "~\\\\%(*SKIP)(*F)|%[sdf]~";
$string = "match %s, %d and %f but NOT with \%s, \%d and \%f.";
if(preg_match_all($regex,$string,$m)) print_r($m);
See live demo
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments