2016-12-02 12 views
-2

私はクイズを出さなければならないので、質問と回答を.txtファイルから読み取る必要があります。選択、テキスト、ラジオ入力など、さまざまなタイプの入力が必要です。ページあたり3ページと8質問があります。.txtファイルから特定のテキストを取得してラジオ入力要素に入力してください

私の質問は以下のとおりです。

  • はどのようにして、ページの区切り文字として(画像を参照してください)brake_page;することができますか?
  • 入力用のテキストのように質問するにはどうすればよいですか?

ここに私の.txtファイルの画像があります。最初は質問で、その後は回答オプションです。

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

+0

テキストファイルを使用しないでください。データベースを使用してください –

答えて

0

文字列にファイルの内容を読む、その後

$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 
//  """, 
// ] 

質問を表現するために、各ページの各ラインを破って:

$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 
// """ 

その後のページでそれを破りますその可能な回答:

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... 
    } 
} 

そして、真剣に、データベースを使用してください。

関連する問題