2016-12-02 44 views
-2

我必須進行測驗,並且需要閱讀.txt文件中的問題和解答。必須有不同類型的輸入,如選擇,文本和無線電輸入,並且每頁都有3頁和8個問題。從.txt文件獲取特定文本並將其放入一個無線電輸入元素

我的問題是:

  • 我怎樣才能讓brake_page;(請參見圖片)作爲頁面分隔符?
  • 如何爲輸入提供文本等問題?

以下是我的.txt文件的圖像,其中第一個是問題,之後是答案選項。

https://i.stack.imgur.com/Q0L1W.png

+0

不要使用文本文件使用數據庫 – 2016-12-02 21:07:03

回答

0

讀取文件內容爲一個字符串:

$contents = file_get_contents('quest.txt'); 
// => """ 
// Question asks why what happens?; 1)Atlantic; 2)Pacific; 3)Mediteran;\n 
// This is another question again?; 1)A; 2)B; 3)C;\n 
// break_page;\n 
// Some other question?; 1)X; 2)Y; 3)Z; 4)fUcK;\n 
// break_page;\n 
// 3rd page question?; 1)Use; 2)A; 3)Database; 4)Instead; 5)Of; 6)This;\n 
// \n 
// """ 

然後通過網頁打破它:

$pages = explode('break_page;', $contents); 
// => [ 
//  """ 
//  Question asks why what happens?; 1)Atlantic; 2)Pacific; 3)Mediteran;\n 
//  This is another question again?; 1)A; 2)B; 3)C;\n 
//  """, 
//  """ 
//  \n 
//  Some other question?; 1)X; 2)Y; 3)Z; 4)fUcK;\n 
//  """, 
//  """ 
//  \n 
//  3rd page question?; 1)Use; 2)A; 3)Database; 4)Instead; 5)Of; 6)This;\n 
//  \n 
//  """, 
// ] 

然後,打破每一行每一頁代表一個問題,其可能的答案:

foreach ($pages as $page) { 
    $lines = array_filter(explode(PHP_EOL, $page)); 
    // => [ 
    // "Question asks why what happens?; 1)Atlantic; 2)Pacific; 3)Mediteran;", 
    // "This is another question again?; 1)A; 2)B; 3)C;", 
    // ] 

    foreach ($lines as $line) { 
     $segments = array_filter(array_map('trim', explode(';', $line))) 
     // => [ 
     // "Question asks why what happens?", 
     // "1)Atlantic", 
     // "2)Pacific", 
     // "3)Mediteran", 
     // ] 

     // Do whatever you want with them... 
    } 
} 

而且,嚴重的是使用數據庫。

相關問題