使用php浏览txt文件,搜索并显示特定内容

使用php浏览txt文件,搜索并显示特定内容,php,Php,我用textfields制作了一个简单的表单,当我提交一个按钮时,它会将所有textfield值写入一个.txt文件。以下是.txt文件内容的示例: ----------------------------- How much is 1+1 3 4 5 1 ----------------------------- 第一行和最后一行是用来分隔数据的。----后面的第一行是问题,底部分隔符(1)前面是正确答案,而问题和正确答案之间的所有值都是错误答案 我现在想做的是分别呼出问题、错误答案和正确答

我用textfields制作了一个简单的表单,当我提交一个按钮时,它会将所有textfield值写入一个.txt文件。以下是.txt文件内容的示例:

-----------------------------
How much is 1+1
3
4
5
1
-----------------------------
第一行和最后一行是用来分隔数据的。
----
后面的第一行是
问题
,底部分隔符(1)前面是
正确答案
,而
问题
正确答案
之间的所有值都是
错误答案

我现在想做的是分别呼出
问题
错误答案
正确答案
,分别是:

 echo $quesiton;
 print_r ($false_answers); //because it will be an array
 echo $true answer;
我认为解决方案是
strpos
,但我不知道如何以我想要的方式使用它。我能做这样的事吗

 Select 1st line (question) after the 1st seperator
 Select 1st line (true answer) before the 2nd seperator
 Select all values inbetween question and true answer
请注意,我只展示了一个示例,.txt文件中有很多这样的问题,用--------分隔

我使用strpos解决这个问题的想法正确吗?有什么建议吗

编辑: 找到一些函数:

$lines = file_get_contents('quiz.txt');
$start = "-----------------------------";
$end = "-----------------------------";

$pattern = sprintf('/%s(.+?)%s/ims',preg_quote($start, '/'), preg_quote($end, '/'));
if (preg_match($pattern, $lines, $matches)) {
    list(, $match) = $matches;
    echo $match;
}
我想这可能行得通,还不确定。

你可以试试这个:

$file = fopen("test.txt","r");
$response = array();
while(! feof($file)) {
    $response[] = fgets($file);
}
fclose($file);
这样,您将得到如下响应数组:

Array(
    [0]=>'--------------',
    [1]=>'How much is 1+1',
    [2]=>'3',
    [3]=>'4',
    [4]=>'2',
    [5]=>'1',
    [6]=>'--------------'
)
你可以试试这个:

$file = fopen("test.txt","r");
$response = array();
while(! feof($file)) {
    $response[] = fgets($file);
}
fclose($file);
这样,您将得到如下响应数组:

Array(
    [0]=>'--------------',
    [1]=>'How much is 1+1',
    [2]=>'3',
    [3]=>'4',
    [4]=>'2',
    [5]=>'1',
    [6]=>'--------------'
)

您可以尝试以下方法:

$lines = file_get_contents('quiz.txt');
$newline = "\n"; //May need to be "\r\n".
$delimiter = "-----------------------------". $newline; 
$question_blocks = explode($delimiter, $lines); 
$questions = array();
foreach ($question_blocks as $qb) {
   $items = explode ($newline, $qb);
   $q['question'] = array_shift($items);  //First item is the question
   $q['true_answer'] = array_pop($items);  //Last item is the true answer
   $q['false_answers'] = $items; //Rest of items are false answers.
   $questions[] = $q;
}
print_r($questions);

您可以尝试以下方法:

$lines = file_get_contents('quiz.txt');
$newline = "\n"; //May need to be "\r\n".
$delimiter = "-----------------------------". $newline; 
$question_blocks = explode($delimiter, $lines); 
$questions = array();
foreach ($question_blocks as $qb) {
   $items = explode ($newline, $qb);
   $q['question'] = array_shift($items);  //First item is the question
   $q['true_answer'] = array_pop($items);  //Last item is the true answer
   $q['false_answers'] = $items; //Rest of items are false answers.
   $questions[] = $q;
}
print_r($questions);

这并不能真正回答问题,但可能是问题的开始。谢谢,我认为这可能与我所寻找的东西类似。这并不能真正回答问题,但可能是问题的开始。谢谢,我认为这可能与我所寻找的东西类似