首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP读取包含Q&A的txt文件并创建数组或对象?

PHP读取包含Q&A的txt文件并创建数组或对象?
EN

Stack Overflow用户
提问于 2020-05-24 19:29:09
回答 1查看 59关注 0票数 0

我在moodle中看到了这种功能,他们上传.txt文件并解析数据并创建问题库。有些问题也有三个选择。

样本:-

代码语言:javascript
复制
What is php?

A. Php is a language.

B. Php creates HTML.

C. Php is fun.

D. none of these.

ANSWER: D



Which is YII-2?

A. Framework.

B. Tool.

C. None of these

ANSWER: A

到目前为止,我已经尝试了解析它,但我不知道到底要做什么来实现它的批量上传。

代码语言:javascript
复制
txt_file    = file_get_contents('path/to/file.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);

foreach($rows as $row => $data)
{

}

数组我正在尝试获取

代码语言:javascript
复制
[
[
'question' => 'Php is a language.',
'options' => [
             'A' =>  'Php is a language.',
             'B' => 'Php creates HTML.',
             'C' => 'Php is fun.',
             'D' => 'none of these.'
             ],
'answer' => 'D'
],
.....
]

样本档案:-文件代码fbSObnlghQkqroEk4lrQ

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-24 22:40:48

多答案逻辑

编辑-宽恕版

试试(?mi)^(?!\h*(?:[A-Z]\h*\.|ANSWER\h*:))\h*(?<question>\S.*?)\??\h*\s*^\h*A\h*\.\h*(?<A>.*?)\h*\s*(?:^\h*B\h*\.\h*(?<B>.*?)\h*(?:\s*^\h*C\h*\.\h*(?<C>.*?)\h*(?:\s*^\h*D\h*\.\h*(?<D>.*?)\h*(?:\s*^\h*E\h*\.\h*(?<E>.*?)\h*)?)?)?)?\s*^\h*ANSWER\h*:\h*(?<answer1>[A-F])(?:\h*,\h*(?<answer2>[A-F]))?

演示

匹配循环,从部件创建单个数组,

附加到更大的数组。

不需要“选项”,但匹配后可以从组A创建子数组。

然后添加到单个数组中。

在下一场比赛中重复。

演示

代码语言:javascript
复制
<?php
// Your code here!

$regex = '~(?mi)^(?!\h*(?:[A-Z]\h*\.|ANSWER\h*:))\h*(?<question>\S.*?)\??\h*\s*^\h*A\h*\.\h*(?<A>.*?)\h*\s*(?:^\h*B\h*\.\h*(?<B>.*?)\h*(?:\s*^\h*C\h*\.\h*(?<C>.*?)\h*(?:\s*^\h*D\h*\.\h*(?<D>.*?)\h*(?:\s*^\h*E\h*\.\h*(?<E>.*?)\h*)?)?)?)?\s*^\h*ANSWER\h*:\h*(?<answer1>[A-F])(?:\h*,\h*(?<answer2>[A-F]))?~'; 

// Declare a string 
$nameString = 'What is php?

A. Php is a language.

B. Php creates HTML.

C. Php is fun.

D. none of these.

ANSWER: D

Linux is
A. A Graphic Software
B. A Driver
C. A Line Controller Software
D. An Operating System
ANSWER: D
Which feature helps in compress and contain the collection of files in to one
A. Zip
B. Shortcut
C. Icon
D. Extract
ANSWER: A


Which is YII-2?

A. Framework.

B. Tool.

C. None of these

ANSWER: A,B'; 


// Use preg_match_all() function

$QA = []; // All questions

if ( preg_match_all($regex, $nameString, $matches, PREG_SET_ORDER) ) {
    foreach ($matches as $match) {
       $qa = [];   // Single question
       $qa['question'] = $match['question'];
       $qa['A'] = $match['A'];
       if ( array_key_exists( 'B', $match ) && $match['B'] )  $qa['B'] = $match['B'];
       if ( array_key_exists( 'C', $match ) && $match['C'] )  $qa['C'] = $match['C'];
       if ( array_key_exists( 'D', $match ) && $match['D'] )  $qa['D'] = $match['D'];
       if ( array_key_exists( 'E', $match ) && $match['E'] )  $qa['E'] = $match['E'];
       $qa['answer1'] = $match['answer1'];
       if ( array_key_exists( 'answer2', $match ) && $match['answer2'] )  $qa['answer2'] = $match['answer2'];

       array_push( $QA, $qa );
    }
} 
else { 
    echo("Could not find a single question");  
} 

print_R ( $QA);

?>

输出

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [question] => What is php
            [A] => Php is a language.
            [B] => Php creates HTML.
            [C] => Php is fun.
            [D] => none of these.
            [answer1] => D
        )

    [1] => Array
        (
            [question] => Linux is
            [A] => A Graphic Software
            [B] => A Driver
            [C] => A Line Controller Software
            [D] => An Operating System
            [answer1] => D
        )

    [2] => Array
        (
            [question] => Which feature helps in compress and contain the collection of files in to one
            [A] => Zip
            [B] => Shortcut
            [C] => Icon
            [D] => Extract
            [answer1] => A
        )

    [3] => Array
        (
            [question] => Which is YII-2
            [A] => Framework.
            [B] => Tool.
            [C] => None of these
            [answer1] => A
            [answer2] => B
        )

)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61991347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档