PHP Read a txt file contains Q & A and create an array or an object?

橙三吉。 提交于 2020-06-29 06:41:39

问题


I have seen this functionality in moodle where they upload .txt files and parse data and create question bank . Some questions have three options as well

Sample :-

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

Till now I have tried this to parse it but I don't know what exactly to be done to achieve it for bulk uploading .

txt_file    = file_get_contents('path/to/file.txt');
$rows        = explode("\n", $txt_file);
array_shift($rows);

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

}

Array I am trying to acquire

[
[
'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'
],
.....
]

Sample file:- File code fbSObnlghQkqroEk4lrQ


回答1:


Multiple answer logic

edit - forgiving version

Try (?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]))?

demo

match in loop, create single array from parts,
append to larger array.
not need 'options' but after match can create sub-array from groups A-E
then add to single array.
repeat on next match.

php_demo

<?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);

?>

Output

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
        )

)


来源:https://stackoverflow.com/questions/61991347/php-read-a-txt-file-contains-q-a-and-create-an-array-or-an-object