假设我有一个无法以任何方式更改的文本文档,需要保留原样。
文本文档可能格式化为的示例:
1. What is soup commonly paired with?
2.
3.
4. Alcohol
5. Water
6. Bread
7. Vegtables
8.
9.
10.
注意:
这些数字不包含在内,但它们用于表示始终存在的单词之间的小空格。
问题
并不总是有问号注2:
问题可能有时候是2行,可能如下所示
0. What is soup
1. commonly paired with?
2.
3.
4. Alcohol
5. Water
6. Bread
7. Vegtables
8.
9.
10.
其他:
那么我究竟如何将它们分隔成一个数组呢?
所以喜欢$ questions []和$ answers []
主要问题是我没有任何内容可以将问题和答案联系起来: 我猜不出它们的确切线 问题并不总是有问号 所以没有什么可以真正链接到它?
答案 0 :(得分:0)
假设您已经将文档中的文本读入变量$text
,您可以通过拆分文本中的第一个空行来分隔问题和答案。
$qAndAs = preg_split('/\n\s*\n/', $text, 2, PREG_SPLIT_NO_EMPTY);
拆分模式是换行符(\n
),零个或多个空格(\s*
)和另一个换行符。
那应该给你一个双元素数组,其中[0]
是问题而[1]
是答案。
如果它没有,那么出现了问题。
if (count($qAndAs) !== 2) {
// The text from the document didn't fit the expected pattern.
// Decide how to handle that. Maybe throw an exception.
}
将它们分开后,您可以从问题中删除任何新行
$question = str_replace(["\r", "\n"], ' ', trim($qAndAs[0]));
并将你的答案分成另一个数组。
$answers = preg_split('/\s*\n\s*/', $qAndAs[1], -1, PREG_SPLIT_NO_EMPTY);
答案 1 :(得分:0)
两个解决方案都在一个文件中接受多个问题/答案。
解决方案1 (类似于此主题中的另一个):
$questions = array();
$answers = array();
//Split text into questions and answers blocks (2 line breaks or more from each other)
$text = preg_split('/\n{2,}/', $text, -1, PREG_SPLIT_NO_EMPTY);
foreach ($text as $key => $value)
{
//0, 2, 4, ... are questions, 1, 3, 5, ... are answers
if ($key % 2)
{
$answers[] = explode("\n", $value);
}
else
{
$questions[] = str_replace("\n", '', $value);
}
}
解决方案2 (从文件中逐行读取难看):
//Open the file
$f = fopen("test.txt","r");
//Initialize arrays of all questions and all answers
$all_questions = array();
$all_answers = array();
$is_question = true;
$last = ''; //contains a previous line
//Iterate over lines
while (true)
{
//Get line
$line = fgets($f);
//Check if end of file
$end = ($line === false);
//Trim current line
$line = trim($line);
if ($line != '')
{
//If the previous line was empty, then reset current question and answers
if ($last == '')
{
$question = array();
$answers = array();
}
//Add line of question or answer
if ($is_question)
{
$question[] = $line;
}
else
{
$answers[] = $line;
}
}
else
{
//If the previous line wasn't empty, or we reached the end of file, then save question / answers, and toggle $is_question
if ($last != '' OR $end)
{
if ($is_question)
{
$all_questions[] = implode(' ', $question); //implode to merge multiline question
$is_question = false;
}
else
{
$all_answers[] = $answers;
$is_question = true;
}
}
}
//Break if end of file
if ($end)
{
break;
}
$last = $line;
}
fclose($f);