如何在PHP中解析Blackberry Messenger CSV?

如何在PHP中解析Blackberry Messenger CSV?,php,excel,blackberry,csv,bb-messenger,Php,Excel,Blackberry,Csv,Bb Messenger,我想从Blackberry Messenger生成的CSV文件中解析并创建一个文件 例如: BlackBerry Messenger,6.0.0.129CRLF 201112071323251405361,"2732E6DB ","555ABCDA",TextData TextData TextData TextDataCRLF 201112071323253558103,"555ABCDA","2732E6DB ",TextData TextData TextDataCRLF 2011120

我想从Blackberry Messenger生成的CSV文件中解析并创建一个文件

例如:

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
日期为YYYYMMDD+历元格式。 每条记录都以CRLF(回车换行)字符结尾,而MessageText中的每一新行都由LF(换行)字符分隔

正如您所看到的,问题在于MessageText不包含在任何容器中,它可能有许多新行。所以我不能直接在excel中导入它

我认为我自己是PHP初学者,就文件操作而言,如果可能的话,请尝试描述代码。


谢谢。

正如@xbonez所说,这是非常棘手的事情。我想你需要一次读每一行。第一列中的日期可能是您确定新邮件开始位置的技巧。这不是PHP代码,而是我将使用的方法:

<?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);     
}

?>

new_bbm_message
函数用于确定该行是否为新bbm消息的开头(如果该行是新消息,则返回TRUE;如果该行是消息的继续,则返回FALSE)。您可以尝试在PHP中将行的前20个字符更改为有效日期,或者测试位置21是否为逗号

您需要再次检查substr调用的一些号码,它们可能已关闭


祝你好运。

这是一个棘手的问题
fgetcsv
不起作用,因为消息也使用逗号,而逗号被读取为分隔符。使用
fgets
逐行阅读,然后用逗号(
explode
)拆分也不起作用,因为消息会溢出到下一行。这看起来不错。但是你能推荐一个功能来检查这行是否是新消息吗?更新:每条消息都以CRLF(回车换行符)字符结尾,而MessageText中的每一新行都由LF(换行符)字符分隔。很高兴您找到了解决方案。“您可以尝试将行的前20个字符更改为PHP中的有效日期,或者测试位置21是否为逗号。”这是我建议的函数。我不打算写整个函数,因为我不知道你会采取哪种方法。
<?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);     
}

?>