+ Reply to Thread
Results 1 to 4 of 4

 

Thread: Split a text file into 2 equal portions

  1. #1
    thin[box]king

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Cambridge
    Posts
    1,905
    Thanks
    13
    Thanked 12 Times in 12 Posts


    I have a large text file that i wish to process with PHP.
    It makes the server generate a 500 error, but if I manually cut the file down to size the script works, is there an easy way for changing:

    file.txt
    to
    file_1.txt and
    file_2.txt

    where file_1 and file_2 have equal line counts.

    Thanks
    Follow Me | Looking for Merchants Who Do Scifi Stuff

  2. #2
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    322
    Thanks
    1
    Thanked 4 Times in 2 Posts
    The following PHP should be able to do it; but there may be a catch 22 if the timeout is a file access issue rather than being related to the processing that you are doing. PHP will need write access to the current directory.

    PHP Code:
    <?php
      $source 
    "file.txt";
      
    $destination1 "file_1.txt";
      
    $destination2 "file_1.txt";

      
    // count the number of lines in $source
      
    $lines 0;
      
    $fs fopen($source,"r");
      while(!
    feof($fs))
      {
        
    fgets($fs,1024);
        
    $lines++;
      }

      
    // divide $count by 2 to get the halfway point
      
    $count intval($count 2);

      
    // rewind $source and open $destination1 & 2 for writing
      
    rewind($fs);

      
    $fd1 fopen($destination1,"w");
      
    $fd2 fopen($destination2,"w");

      
    $progress 0;
      
      while(!
    feof($fs))
      {
         
    $line fgets($fs,1024);
         
    $progress++;
         if (
    $progress $count)
         {
            
    fwrite($fd1,$line);
         }
         else
         {
            
    fwrite($fd2,$line);
         }
      }

      
    fclose($fs);
      
    fclose($fd1);
      
    fclose($fd2);
    ?>
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  3. #3
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    661
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nice script, but is this line correct?

    // divide $count by 2 to get the halfway point
    $count = intval($count / 2);
    shouldn't it be
    // divide $count by 2 to get the halfway point
    $count = intval($lines / 2);
    probably just me.

  4. #4
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    322
    Thanks
    1
    Thanked 4 Times in 2 Posts
    No, it's not just you, it's a bug...
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

+ Reply to Thread


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Redirecting ASP to another file on Apache
    By DanielCoe in forum Programming
    Replies: 1
    Last Post: 04-08-04, 04:14 PM
  2. Dell text link
    By will 68 in forum TradeDoubler
    Replies: 2
    Last Post: 12-08-03, 05:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2