2013-06-25 70 views
0

我有一個PHP腳本,我用它來解析一些純文本到CSV格式。解決PHP解析純文本?

<?php 
    $text = "1. Bonus: Name some things about US history. For 10 points each: 
[10] Name the first president of the United States of America. 
ANSWER: George Washington 
[10] How many original colonies were there? 
ANSWER: 13 
[10] How many states exist today? 
ANSWER: 50"; 


function text_to_csv($text = null) { 
    $lines = explode("\n", $text); 
    $data = array(); 
    $temp = array(); 
    foreach($lines as $line) { 
    $line = trim($line); 
    if (empty($line)) { 
     continue; 
    }  
    if (preg_match('/^\[10\](.+?)$/', $line, $quest)) { 
     $temp[] = trim($quest[0]); 
     continue; 
    } 
    if (preg_match('/^([0-9]+)\.(.+?)$/', $line, $quest)) { 
     $temp[] = trim($quest[1]); 
     $temp[] = trim($quest[2]); 
     continue; 
    } 
    if (preg_match('/^ANSWER\:(.+?)$/', $line, $quest)) { 
     $temp[] = trim($quest[1]); 
     $data[] = "|".implode('|,|', $temp)."|"; 
     $temp = array(); 
    } 

    } 

    return implode("\r\n", $data); 
} 

echo text_to_csv($text); 
?> 

這將返回:

|1|,|Bonus: Name some things about US history. For 10 points each:|,|[10] Name the first president of the United States of America.|,|George Washington| 
|[10] How many original colonies were there?|,|13| 
|[10] How many states exist today?|,|50| 

第二和第三[10]是在不同的行並且不與所述第一重合。我想要的輸出是:

|1|,|Bonus: Name some things about US history. For 10 points each:|,|[10] Name the first president of the United States of America.|,|George Washington|,|[10] How many original colonies were there?|,|13|,|[10] How many states exist today?|,|50| 

整個字符串都在一行上,並用逗號分隔。我認爲正在發生的事情是腳本將第二個和第三個[10]視爲新條目,而不是連接到前一個數組。任何人都可以幫我解決這個問題。這將不勝感激!

+0

忽略顏色。我不想讓他們表現出來。 – user2483916

+0

@Robert你能幫忙解釋一下嗎?我相當新手PHP腳本。 – user2483916

+0

你想要什麼輸出格式? –

回答

1

某些文字有一個簡單的回車符\r,其他一個換行符\n,其他人有回車符和換行符\r\n。這取決於用於創建文本的編輯器。

您需要覆蓋這些可能的情況。這樣做:

return implode("\r",implode("\n",implode("\r\n",$data)));

0

你可以做到這一點,而無需使用破滅,甚至臨時陣列ECT,只需使用字符串連接。最有可能更快,但最重要的是你。

<?php 
$text = "1. Bonus: Name some things about US history. For 10 points each: 
[10] Name the first president of the United States of America. 
ANSWER: George Washington 
[10] How many original colonies were there? 
ANSWER: 13 
[10] How many states exist today? 
ANSWER: 50"; 

function text_to_csv($text = null){ 
    $data = null; 
    $lines = explode("\n",trim($text)); 

    foreach($lines as $line) 
    { 
     $line = trim($line); 
     if(empty($line)) 
     { 
      continue; 
     } 
     if(preg_match('/^\[10\](.+?)$/', $line, $quest)) 
     { 
      $data .= "|".trim($quest[0])."|,"; 
     } 
     if(preg_match('/^([0-9]+)\.(.+?)$/', $line, $quest)) 
     { 
      $data .= "|".trim($quest[1])."|,"; 
      $data .= "|".trim($quest[2])."|,"; 
     } 
     if(preg_match('/^ANSWER\:(.+?)$/', $line, $quest)) 
     { 
      $data .= "|".trim($quest[1])."|,"; 
     } 
    } 
    return rtrim($data, ','); 
} 

echo text_to_csv($text); 

/* 
|1|,|Bonus: Name some things about US history. For 10 points each:|,|[10] Name the first president of the United States of America.|,|George Washington|,|[10] How many original colonies were there?|,|13|,|[10] How many states exist today?|,|50| 
*/ 
?> 
+0

如果我在$ text變量中放置了兩套獎金,這種工作是否會奏效? – user2483916

+0

它會構建與implode完全相同的輸出,但不會執行不必​​要的數組處理。不知道你的獎金是什麼意思,它只是給我一個字符串 –