View Single Post
  #5 (permalink)  
Old 07-03-2007, 08:32 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
How about this?

Well, it's not a tutorial, but how's this?

PHP Code:
<?php
$target_file 
'source.txt';
$output_file 'target.txt';
//Open the target file for read-only mode.
$fh = @fopen($target_file,'r');
if (
$fh) {
  
//Open the output file for writing.
  
$ofh = @fopen($output_file,'w');
  
$nl_prefix ""//Will hold newline as a prefix for all lines after the first
  
while (!feof($fh)) {
    
$a_line fgets($fh); //Get a full line until a newline is received.
    
$a_line trim($a_line); //Remove any extraneous space that may be present on a blank line
    
if (strlen($a_line) > 0) { //This was an empty line, so skip processing.
      //Now, do what you want to the data.  In this case, it's parsing SQL, so let's use preg_match
      
preg_match_all('/([a-z0-9\_\-]+) = `([^`]+)`/i',$a_line,$matches);      
      
      if (
count($matches[1]) > 0) { //Then, there are matches
        //Now, generate a new output line (id:name:password)
        
$output_line $nl_prefix.$matches[2][0].':'.$matches[2][1].':'.$matches[2][2];        
        
        
//Then, write it to the new file.
        
fwrite($ofh,$output_line);
        
        
//Set newline prefix for non-first lines.
        
$nl_prefix "\n";
      }
    }
  }
  
  
//Cleanup by closing each of the open files
  
fclose($fh);
  
fclose($ofh);
} else {
  echo 
'Unable to open file';
}
?>
Oh, let me point out that to customize this for your own usage which may have a different file format, you "just" need to edit the preg_match_all line to the RegEx of your choice.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by TeraTask : 07-03-2007 at 08:35 PM. Reason: Forgot to tell how to customize!
Reply With Quote