2016-12-02 17 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... 
    } 
} 

而且,严重的是使用数据库。

相关问题