0

我想从 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

如您所见,CSV 文件具有以下格式

Date,SendersPIN,ReceiversPIN,MessageText

日期为 YYYYMMDD+epoch 格式。每条记录都以 CRLF(回车换行)字符结尾,而 MessageText 中的每个新行由 LF(换行)字符分隔。

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

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

谢谢你。

4

1 回答 1

0

正如@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)。您可以尝试将行的前 20 个字符更改为 PHP 中的有效日期,或者测试位置 21 是否为逗号。

您需要仔细检查 substr 调用的一些数字,它们可能已关闭。

祝你好运。

于 2012-01-25T21:28:02.313 回答