我有一个文本文件,其中包含以下信息:
1. What color is the water?
Red
*Blue
Green
Orange
2. Stack Overflow is awesome.
*True
False
3. Explain why you love code:我希望输出是这样的:
MC What color is the water? Red Incorrect Blue Correct Green Incorrect Orange Incorrect
TF Stack Overflow is awesome. True
ES Explain why you love code:问题1和问题2中的*表示正确的答案。基本上,我试图创建PHP代码,它可以识别问题的类型,并将其标记为MC (选择题)、TF (真/假)和ES (散文)。我不知道该怎么处理。如能提供任何协助,将不胜感激。
发布于 2015-09-18 03:29:14
这里有一些更多的描述,说明您可以如何做到这一点,以及一些您可以修改的代码。请注意,这不是生产级别代码。
理论
在这一点上,我们的问答被填充为一个关联的数组,如下所示:
Array
(
[1. What color is the water?] => Array
(
[0] => Red
[1] => *Blue
[2] => Green
[3] => Orange
)
[2. Stack Overflow is awesome.] => Array
(
[0] => *True
[1] => False
)
[3. Explain why you love code:] =>
)现在我们循环通过问题,得到答案是ES,TF还是MC类型。我们还很好地格式化了答案。然后展示出来。
Practice
创建一个读取文件并为我们创建这种数组的函数。
/**
* Get questions and answers from file
*
* @param $filename string Name of the file along with relative or absolute path
*
* @returns array Associated array of 'question1'=>array-of-answers, 'question2'=>array2-of-answers
*/
function getQuestionsAndAnswers($filename)
{
$file = @fopen($filename, 'r');
$quiz = array(); // associated array containing '1. question1'=>answer array
$answers = array(); // temporarily holds answers
$question = ''; // temporarily holds questions
while (($line = fgets($file)) != null)
{
$line = trim($line);
if ($line === '') continue;
if (preg_match('/^\d+\. /', $line) === 1)
{
if (count($answers) > 0)
{
$quiz[$question] = $answers;
$answers = array();
}
$question = $line;
$quiz[$question] = '';
}
else
{
$answers[] = $line;
}
}
if (count($answers) > 0)
{
$quiz[$question] = $answers;
}
return $quiz;
}创建一个函数来获取答案,并告诉我们答案的类型。
/**
* Get answer type short code from answer array
*
* @param $answer array Answers
*
* @returns string Short code answer type (e.g. ES for Essay, TF for True/False, MC for multiple choice)
*/
function answerType($answer)
{
if (!is_array($answer)) return 'ES';
$flattenedAnswer = implode(',', $answer);
if (stripos($flattenedAnswer, 'true') !== false) return 'TF';
return 'MC';
}创建一个很好地格式化我们的答案的函数。
/**
* Format answers based on answer type
*
* @param $answer array Answers
* @param $answerType string Short code of answer type
*
* @returns string Formatted answer
*/
function answers($answer, $answerType)
{
if ($answerType === 'ES') return $answer;
if ($answerType === 'TF')
{
foreach ($answer as $x)
{
if (strpos($x, '*') === 0) return substr($x, 1);
}
return '';
}
$flattenedAnswer = '';
foreach ($answer as $x)
{
if (strpos($x, '*') === 0)
{
$flattenedAnswer .= ' ' . substr($x, 1) . ' Correct';
}
else
{
$flattenedAnswer .= " $x Incorrect";
}
}
return $flattenedAnswer;
}现在我们有了我们的基础部分,让我们把所有的东西放在一起。
// $quiz will be populated as an array that looks like this
// 'question1'=>array('answer1', 'answer2')
// 'question2'=>array('answer1', 'answer2') etc
$quiz = getQuestionsAndAnswers('./questions.txt');
// loop through all questions and use functions to format the output
foreach ($quiz as $question=>$answer)
{
$answerType = answerType($answer);
echo sprintf("%s %s %s\n",
$answerType,
$question,
answers($answer, $answerType)
);
}让我们运行代码并查看输出:
$ php questions.php
MC 1. What color is the water? Red Incorrect Blue Correct Green Incorrect Orange Incorrect
TF 2. Stack Overflow is awesome. True
ES 3. Explain why you love code:太棒了。看来一切都很顺利。我希望这段代码是self注释的,但是如果您有问题,可以随意提问。
发布于 2015-09-18 02:02:56
要回答您的问题,您可以使用.ini文件,而不是.txt文件。检查这个文件,它返回一个数组。或者如果您真的需要.txt,您可以使用目录 (它返回一个字符串).
或者你可以试试;数据库。你觉得哪个好。
发布于 2015-09-18 03:01:12
理想情况下,它应该位于一个数据库中,其中包含两个表--问题和答案--其中的关系是问号。您可以使用file_get_contents(),但是需要大量的编码。如果您想要使用平面文件,您应该计划使用:分隔格式,其中每个问题被除以":“(例如),问题的每个部分被",”和每个答案除以";“第一个答案是正确的,file_get_contents()加载文本文件并将问题、部分和答案划分为数组,并使用"for”语句循环每个数组。例如:水是什么颜色?,蓝色;红色;绿色;橙色:堆栈溢出太棒了。对;假:解释为什么你喜欢代码:
https://stackoverflow.com/questions/32642324
复制相似问题