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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.