HotshotONE Posted June 18, 2007 Share Posted June 18, 2007 I'm very bad with preg_match and the string syntax. I'm getting better slowly though Here is my problem: I have a .txt file that I can open and search. It contains lines like this: "INSERT INTO `x_system` VALUES (325508,-99,-6,3,14878,'=^..^=',11483,'shelly1205',0,'',426); INSERT INTO `x_system` VALUES (325513,-94,-6,1,70052,'Byzantium',11943,'AbleMan',973,'TF2',607); INSERT INTO `x_system` VALUES (325518,-89,-6,3,16271,'Hesterville',12638,'Hester49',432,'TPE',473);" I have that pulled from the file and named $cont. I want to pull out all records for example that are for the user "AbleMan" so I can take certain parts of those numbers and put them into my own SQL database. There may be multiple records for "AbleMan". The file contains thousands of records so I need to be able to search it. Here is my preg_match_all: preg_match_all( "/INSERT INTO `(.*)` VALUES \((.*),(.*),(.*),(.*),(.*),'(.*)',(.*),'AbleMan',(.*),'TF2',(.*)\);/si", $cont, $match ); The problem is that when I match that value it returns all the lines and doesn't distinguish between a line that contains the name I am searching for. I want to just pick the lines up that contain the user "AbleMan" from the place where it starts "Insert" to the end. I can simply just replace the "x_system" for my own table name and insert that exactly as given. Any help figuring out how to grab only the desired match line would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 18, 2007 Share Posted June 18, 2007 If the location of "AbleMan" is not crucial, use a simple grep command (if you're on a non-Windows machine): $ grep AbleMan file_name Otherwise, try the /U modifier to make your quantifiers ungreedy. Quote Link to comment Share on other sites More sharing options...
HotshotONE Posted June 18, 2007 Author Share Posted June 18, 2007 Ok, the U modifier helps cut the trailing stuff off. But it still returns the line before. The "^" modifier forces it to start at that line correct? Out of the above data the current: preg_match_all( "/INSERT INTO `(.*)` VALUES \((.*),(.*),(.*),(.*),(.*),'(.*)',(.*),'AbleMan',(.*),'TF2',(.*)\);/siU", $cont, $match ); Returns: INSERT INTO `x_world` VALUES (325508,-99,-6,3,14878,'=^..^=',11483,'shelly1205',0,'',426); INSERT INTO `x_world` VALUES (325513,-94,-6,1,70052,'Byzantium',11943,'AbleMan',973,'TF2',607); I want it to just return: INSERT INTO `x_world` VALUES (325513,-94,-6,1,70052,'Byzantium',11943,'AbleMan',973,'TF2',607); Quote Link to comment Share on other sites More sharing options...
effigy Posted June 19, 2007 Share Posted June 19, 2007 <pre> <?php $sql = <<<SQL "INSERT INTO `x_system` VALUES (325508,-99,-6,3,14878,'=^..^=',11483,'shelly1205',0,'',426); INSERT INTO `x_system` VALUES (325513,-94,-6,1,70052,'Byzantium',11943,'AbleMan',973,'TF2',607); INSERT INTO `x_system` VALUES (325518,-89,-6,3,16271,'Hesterville',12638,'Hester49',432,'TPE',473);" SQL; preg_match_all( "/INSERT INTO `[^`]+` VALUES \((?:.+?,){7}'AbleMan'.+?\);/i", $sql, $matches); print_r($matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.