How to parse Blackberry Messenger CSV in PHP?

588 Views Asked by At

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.

1

There are 1 best solutions below

2
On

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.