Jump to content

Preg_Match_All Help


HotshotONE

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/56100-preg_match_all-help/
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/56100-preg_match_all-help/#findComment-277186
Share on other sites

<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>

Link to comment
https://forums.phpfreaks.com/topic/56100-preg_match_all-help/#findComment-277610
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.