PHP Extract for DB Insert

Discussion in 'PHP' started by IGiveMoney, Aug 2, 2008.

  1. #1
    Ok so I have been futzing with this for a while tonight
    and I cant just get this right...

    So here it goes.

    I have some text files on my server. I would like a script
    to open the file and extract the data within in and set it
    up for an insert into a database.

    Let me give you an example.

    These are just standard text files so...


    Couple things about that are...
    1.) Notice the spacing in between the end of one offer
    details and the title of the next one.

    2.) Would like to get the insert to be

    INSERT INTO offers (offer_name, credit, cost, cr_time, site_id) VALUES('Blockbuster','1','9.99','Within 1 Day - 3 Days','32');

    So the script would quickly loop thru the file(s) and remove
    all the data except for the above example and then output the loop
    to an insert.

    I can get the file open part and lil trouble with the loop and I can
    handle the insert part. Its getting the right data to be extracted for
    formatting that is driving me mad.

    Maybe I need a clear mind and rested eyes :rolleyes:


    PS: The site id would be called from the file name as it opens
    the file for the loop.
     
    IGiveMoney, Aug 2, 2008 IP
  2. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    should be able to do something like this w/
    (of course file will be loaded into $filetext via fopen or what ever methodyou wish)
    
    $offers = explode("\n\n",$filetext);
    foreach ($offers as $offer) {
        $lines = explode("\n",trim($offer));
        foreach ($lines as $line) {
            $error=0;
            $name = $line[0];
            if (preg_match("/:([\S]+) Credit/",$line[1],$match)) {
                $credits = $match[1];
            } else {
                $error=1;
            }
            if (preg_match("/:([\S\s]+)$/",$line[2],$match)) {
                $cost = $match[1];
            } else {
                $error=1;
            }
            if (preg_match("/:([\S\s]+)$/",$line[3],$match)) {
                $cr_time = $match[1];
            } else {
                $error=1;
            }
            if ($error==0){
                # SQL INSERT
            }
        }
    }
    
    PHP:
    Dont have time to test it but should be working if you have an issue let me know
     
    Hallent, Aug 2, 2008 IP
  3. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so my updated codes is as follow:

    
    
    <?php
    $filetext = "rb.txt";
    $fh = fopen($filetext, 'r+');
    $contents = fread($fh, filesize($filetext));
    fclose($fh);
    
    $offers = explode("\n\n",$contents);
     foreach ($offers as $offer) {
        $lines = explode("\n",trim($offer));
    	$credits="";
            $cost="";
    	$cr_time="";
    	
    	$cred_flag=0;
            $cost_flag=0;
            $cr_flag=0;
    	  
        $name=$lines[0];
    	
        foreach ($lines as $line) {
    	     
            if (preg_match("/:([\S]+) Credit/",$line[1],$match)) {
                $credits = $match[1];
    			$cred_flag = 1; 
    		} 
    		
            if (preg_match("/:([\S\s]+)$/",$line[1],$match)) {
                $cost = $match[1];
    			$cost_flag = 1; 
            }
    
            if (preg_match("/:([\S\s]+)$/",$line[1],$match)) {
                $cr_time = $match[1];
    			$cr_flag = 1; 
            }
    
      }
    		echo $name . '<br>';
    	    echo $credits . '<br>'; 
    
       }
    
    ?>
    
    
    PHP:
    My issue:

    $name does just fine and returns all the offer titles
    $credits
    $cost
    $cr_time

    All flops - arent returning anything...

    is the regex correct? Or am I missing something else?
     
    IGiveMoney, Aug 2, 2008 IP
  4. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    oops

    change those to

    if (preg_match("/:([\S]+) Credit/iU",$line[1],$match)) {
                $credits = $match[1];
                $cred_flag = 1; 
            } 
            
            if (preg_match("/:([\S\s]+)$/iU",$line[1],$match)) {
                $cost = $match[1];
                $cost_flag = 1; 
            }
    
            if (preg_match("/:([\S\s]+)$/iU",$line[1],$match)) {
                $cr_time = $match[1];
                $cr_flag = 1; 
            }
    PHP:
     
    Hallent, Aug 2, 2008 IP
  5. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    shoot messed up again
     if (preg_match("/:([\S]+) Credit/iU",$line[1],$match)) {
                $credits = $match[1];
                $cred_flag = 1; 
            } 
            
            if (preg_match("/:([\S\s]+)$/iU",$line[2],$match)) {
                $cost = $match[1];
                $cost_flag = 1; 
            }
    
            if (preg_match("/:([\S\s]+)$/iU",$line[3],$match)) {
                $cr_time = $match[1];
                $cr_flag = 1; 
            }
    PHP:
     
    Hallent, Aug 2, 2008 IP
  6. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    man im super tired..... give me a minute i messed up this whole thing lol ill use your code you have there and fix it
     
    Hallent, Aug 2, 2008 IP
  7. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Oic it was from your changes :)

    use this ....
    there are goign to be somethings missing as im going to bed soon and not adding as i added them in my previous script.... such as trimming the fileinput

    <?php
    $filetext = "rb.txt";
    $fh = fopen($filetext, 'r+');
    $contents = fread($fh, filesize($filetext));
    fclose($fh);
    
    $offers = explode("\n\n",$contents);
    foreach ($offers as $offer) {
        $lines = explode("\n",trim($offer));
        $credits="";
            $cost="";
        $cr_time="";
       
        $cred_flag=0;
            $cost_flag=0;
            $cr_flag=0;
          
        $name=$lines[0];
            
        if (preg_match("/:([\S]+) Credit/iU",$line[1],$match)) {
            $credits = $match[1];
            $cred_flag = 1;
        }
       
        if (preg_match("/:([\S\s]+)$/iU",$line[2],$match)) {
            $cost = $match[1];
            $cost_flag = 1;
        }
    
        if (preg_match("/:([\S\s]+)$/iU",$line[3],$match)) {
            $cr_time = $match[1];
            $cr_flag = 1;
        }
    
    
        echo $name . '<br>';
        echo $credits . '<br>';
    
    }
    
    ?>
    PHP:
     
    Hallent, Aug 2, 2008 IP
  8. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hallent:

    Thanks for the attempt and I too am tired! :(

    The regex is still not working properly its only pulling in the names ok.

    It is kind of important I get it working within this format, because I do a
    linux -dump page to get the data as shown above and the format will
    be the same for all files ran thru this. So if we can get it straight
    it would be awesome.

    As is stands right now, your working example and my working example
    produce the same results - just the offer names

    Thanks!
     
    IGiveMoney, Aug 3, 2008 IP
  9. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    LOL i removed a loop you had and left the singular names for the arrays! DOH!!!!! enjoy actually tested....

    
    <?php
    $filetext = "rb.txt";
    $fh = fopen($filetext, 'r+');
    $contents = fread($fh, filesize($filetext));
    fclose($fh);
    
    $offers = explode("\n\n",$contents);
    foreach ($offers as $offer) {    
        $lines = explode("\n",trim($offer));
        
        $credits="";
            $cost="";
        $cr_time="";
       
        $cred_flag=0;
            $cost_flag=0;
            $cr_flag=0;
         
        $name=$lines[0];
           
        if (preg_match("/:([\S]+) Credit/iU",$lines[1],$match)) {
            $credits = trim($match[1]);
            $cred_flag = 1;
        }
       
        if (preg_match("/:([\S\s]+)$/iU",$lines[2],$match)) {
            $cost = trim($match[1]);
            $cost_flag = 1;
        }
    
        if (preg_match("/:([\S\s]+)$/iU",$lines[3],$match)) {
            $cr_time = trim($match[1]);
            $cr_flag = 1;
        }
    
    
        echo $name . '<br>';
        echo $credits . '<br>';
        echo $cost.'<br>';
        echo $cr_time.'<br>';
        echo "\n";
    
    }
    
    ?>
    
    PHP:
     
    Hallent, Aug 3, 2008 IP