0

I want to parse through and create a single file from CSV files generated from Blackberry Messenger.

An example :

BlackBerry Messenger,6.0.0.129CRLF
201112071323251405361,"2732E6DB ","555ABCDA",TextData  TextData TextData TextDataCRLF
201112071323253558103,"555ABCDA","2732E6DB ",TextData TextData TextDataCRLF
201112071323253746576,"2732E6DB ","555ABCDA",TextData TextData TextData
LF
TextData TextDataLF
TextData TextData TextData TextData TextData TextData TextData TextData TextDataLF
TextData TextData TextData TextDataCRLF
201112071323253809444,"2732E6DB ","555ABCDA", TextData TextData TextData TextDataCRLF
201112091323441592335,"2732E6DB ","555ABCDA", TextData TextDataLF
    LF
<3<3=.>:O :s=.>=) <3<3=.>:O :s=.>=)LF
    LF
- Copy all smiley aboveLF
- pasteLF
- erase 4 dotLF
- send me backLF
- see the magic (smiley change)LF
    CRLF

As you can see that CSV file has following format

Date,SendersPIN,ReceiversPIN,MessageText

Date is in YYYYMMDD+epoch format. Every record ends with a CRLF (carriage return line feed) characters, while each new line in MessageText is separated by LF (line feed) character.

As you can see the problem is that MessageText is not contained in any container and it may have many new lines. So I cant import it in excel directly.

I would consider myself beginner in PHP as far as file operations are concerned, so if possible try to describe the code.

Thank you.

dkcreatto
  • 1
  • 2
  • This is a tricky one. `fgetcsv` doesn't work because the message uses commas as well, which gets read as delimiter. Reading line by line using `fgets` and then splitting on comma (`explode`) doesn't work as well, because messages spill over to next lines. – Ayush Jan 25 '12 at 08:10

1 Answers1

0

As @xbonez says, this is very tricky to do. I think you need to read each line at a time. The date in the first "column" may be your trick to determining where a new message begins. This won't be PHP code, but the method I would use:

<?php
$handle = open($filename,"r");
$input_line = fgets($handle); // Reads the first line of the file, which we will discard
$input_line = fgets($handle); //read a line of text from the file
$msg_data = '';
$mesg = '';
$first_run = TRUE;
while (!$input_line) // Check whether we've reached the end of the file, 
                     // fgets returns FALSE if we have 
{
  if (new_bbm_message($input_line))
  {

    if(!$first_run) 
    //You now have a complete message, do something with it. I'm just going to print it.
      echo $msg_data .  addslashes($mesg);
    else $first_run = FALSE;

    // Get the new messages data
    $msg_data = substr($input_line,0,46); // Get string with Date,SendersPIN,RecieversPIN up to the ',' after receivers pin.
    $mesg = substr($input_line,47);
  } 
  else 
  {
  // This is another line of text of the message.
    $mesg .= $input_line;
  }  

  // Read in the next line of text.
  $input_line = fgets($handle);     
}

?>

The new_bbm_message function is something you get to write to determine if the line is the start of a new BBM message (returns TRUE if the line is a new message or FALSE if it is the continuation of the message one). You can either try to change the first 20 characters of the line into a valid date in PHP, or test whether position 21 is a comma.

You need to double check some of the numbers for the substr calls, they may be off.

Good Luck.

Core.B
  • 662
  • 3
  • 11
  • That looks good. But can you suggest a function which can check if the line is a new message? UPDATE: Every message ends with a CRLF (carriage return line feed) characters, while each new line in MessageText is separated by LF (line feed) character. – dkcreatto Jan 29 '12 at 12:13
  • Glad you found a solution. "You can either try to change the first 20 characters of the line into a valid date in PHP, or test whether position 21 is a comma." was my suggested function. I wasn't going to write the whole function since I didn't know which approach you would take. – Core.B Jan 30 '12 at 18:56