$re = '/insert\s+into\s+historicopreco|insert\'\);\s+(\w+\b\.)+(add\(\')?into(\'\);\s+)?(\w+\b\.)+(add\(\')?historicopreco|update\s+h(istorico)?p(reco)?|update\'\);\s+(\w+\b\.)+(add\(\')?h(istorico)?p(preco)?/i';
$str = 'insert into historicopreco
update historicopreco
insert into
historicopreco
update
historicopreco
update hp
insert into hisoricopreco
update
hp
sql.add(\'update\');
sql.add(\'hp
update\');
aaaa.sql.add(\'historicopreco
sql.add(\'insert\');
aaa.sql.add(\'into\');
aaa.sql.add(\'historicopreco\');';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php