-1

Please assist with the following:

I use the following regex pattern to catch all values inside MySQL queries (["'])(?:(?=(\\?))\2.)*?\1 This works perfectly fine when run on regex online tester https://regex101.com/r/tysxYk/1

But when i use it inside php it returns nothing and can't figure out what's wrong

<?php
    
$query = <<<EOT
    UPDATE `wp_options` SET `option_value` = 'a:1151:{s:15:\"activ";}' WHERE `option_name` = 'my_options';

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
EOT;
    
$status = preg_match( '#(["\'])(?:(?=(\\?))\2.)*?\1#', $query, $matches );
print_r($matches);

Thanks for your support !

Barmar
  • 741,623
  • 53
  • 500
  • 612
kole23
  • 55
  • 4
  • 1
    I think you should double the backslashes, see the regex101 generated code here https://regex101.com/r/tysxYk/1/codegen?language=php – The fourth bird Dec 21 '20 at 16:56

1 Answers1

0

Regex101 has it's own code-generator, using the provided link, that will generate:

<?php
$re = '/(["\'])(?:(?=(\\\\?))\2.)*?\1/m';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

var_dump($matches);
  1. Regex Flags (Optionally)
  2. preg_match_all instead off preg_match
0stone0
  • 34,288
  • 4
  • 39
  • 64