Reputation: 127
I'm trying to find the CREATE SQL queries from a file, to simplify the task I wrote #end
at the end of each query, but the search continues and I'm getting a few results in one array index (or simply a continuation of the file), instead of one.
preg_match_all('/CREATE.*#end/su', $sql, $queries);
SQL example:
CREATE TABLE `tblname` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`label` varchar(32) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `label` (`label`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8#end
Upvotes: 2
Views: 62
Reputation: 522382
The problem is likely that *
is greedy by default. That means it finds the longest match it can, not the shortest. Make it ungreedy: .*?
, now it will stop matching as soon as it finds a single match.
Upvotes: 2