PHP - 解析 *.TXT 文件
PHP - Parsing a *.TXT File
我有一个包含以下信息的文本文件:
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 为 True/False,ES 为论文标记。我不确定如何接近这个。如有任何帮助,我们将不胜感激。
回答您的问题;您可以使用 .ini
个文件,而不是 .txt
个文件。检查这个 parse_ini_file it return an array. Or if you really need .txt
you could use file_get_contents (它 return 一个字符串)...
或者你可以试试;一个数据库。哪个更好。
理想情况下,它应该在一个有两个表问题和答案的数据库中,其中关系是问题编号。您可以使用 file_get_contents() 但是,需要大量编码。如果你想使用平面文件,你应该计划使用:
分隔格式,其中每个问题除以“:”(例如),问题的每个部分除以“,”,每个答案除以“;”第一个答案是正确的, file_get_contents() 加载文本文件, explode() 将问题、部分和答案分成数组,并使用 "for" 语句循环每个数组。例如。
水是什么颜色的?,蓝色;红色;绿色;橙色:Stack Overflow 很棒。,True;False:解释为什么你喜欢代码:
这里有一些关于如何执行此操作的详细说明以及一些您可以修改的代码。请注意,这不是生产级代码。
理论
- 逐行读取文件
- 如果行以(即匹配)数字开始,然后是点,然后是 space,请记住数组中的问题作为键
- 如果行不匹配,记住答案数组中的每一行
- 当我们点击匹配的下一行时,将迄今为止收集到的所有答案与前一个问题相关联
- 循环完成后,我们可能仍然有一个充满答案的数组。如果我们这样做,请将其与我们阅读的最后一个问题联系起来
此时,我们将测验问题和答案填充为关联数组,如下所示:
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 类型。我们还很好地格式化了答案。然后显示出来。
练习
创建一个读取文件并为我们创建这种数组的函数。
/**
* 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:
太棒了。看起来一切顺利。我希望代码是自我评论的,但如果您有任何疑问,请随时提出。
我有一个包含以下信息的文本文件:
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 为 True/False,ES 为论文标记。我不确定如何接近这个。如有任何帮助,我们将不胜感激。
回答您的问题;您可以使用 .ini
个文件,而不是 .txt
个文件。检查这个 parse_ini_file it return an array. Or if you really need .txt
you could use file_get_contents (它 return 一个字符串)...
或者你可以试试;一个数据库。哪个更好。
理想情况下,它应该在一个有两个表问题和答案的数据库中,其中关系是问题编号。您可以使用 file_get_contents() 但是,需要大量编码。如果你想使用平面文件,你应该计划使用: 分隔格式,其中每个问题除以“:”(例如),问题的每个部分除以“,”,每个答案除以“;”第一个答案是正确的, file_get_contents() 加载文本文件, explode() 将问题、部分和答案分成数组,并使用 "for" 语句循环每个数组。例如。 水是什么颜色的?,蓝色;红色;绿色;橙色:Stack Overflow 很棒。,True;False:解释为什么你喜欢代码:
这里有一些关于如何执行此操作的详细说明以及一些您可以修改的代码。请注意,这不是生产级代码。
理论
- 逐行读取文件
- 如果行以(即匹配)数字开始,然后是点,然后是 space,请记住数组中的问题作为键
- 如果行不匹配,记住答案数组中的每一行
- 当我们点击匹配的下一行时,将迄今为止收集到的所有答案与前一个问题相关联
- 循环完成后,我们可能仍然有一个充满答案的数组。如果我们这样做,请将其与我们阅读的最后一个问题联系起来
此时,我们将测验问题和答案填充为关联数组,如下所示:
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 类型。我们还很好地格式化了答案。然后显示出来。
练习
创建一个读取文件并为我们创建这种数组的函数。
/**
* 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:
太棒了。看起来一切顺利。我希望代码是自我评论的,但如果您有任何疑问,请随时提出。