Skip to content
Advertisement

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

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 :-

JavaScript

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 .

JavaScript

Array I am trying to acquire

JavaScript

Sample file:- File code fbSObnlghQkqroEk4lrQ

Advertisement

Answer

Multiple answer logic

edit – forgiving version

Try (?mi)^(?!h*(?:[A-Z]h*.|ANSWERh*:))h*(?<question>S.*?)??h*s*^h*Ah*.h*(?<A>.*?)h*s*(?:^h*Bh*.h*(?<B>.*?)h*(?:s*^h*Ch*.h*(?<C>.*?)h*(?:s*^h*Dh*.h*(?<D>.*?)h*(?:s*^h*Eh*.h*(?<E>.*?)h*)?)?)?)?s*^h*ANSWERh*: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

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement