I want to read uploaded word file by client, using laravel . I am working on an Online examination system and I want to allow teachers to be able to import questions from word file. the content of the file will contain questions and the answers. if the line contains question mark (?) I will consider this line as a question , and the followed lines till the next question will be considered as an answer option. like this
what is the capital of USA?
NewYork
*Washington
Texas
what is the Capital of UAE?
DUBAI
*ABU Dhabi
Alriadh
the answer which has * in the line that means this is the correct answer of option. I can do all of these tasks by uploading text files. I used this code to handle a text file
public function import_text()
{
$questions = [];
$i = 0;
$questionsFile = fopen( 'test.txt', 'r');
while ($line = fgets($questionsFile)) {
if ($line === "\n") {
$i++;
continue;
}
if (!isset($questions['questions'][$i])){
$questions['questions'][$i] = [
'id' => rand(1596805341210, 9999999999999),
'type' => 'Multiple Choice Single Answer',
'question' => '',
'answer_options' => []
];
}
if (preg_match("/(.)+\?/", $line)) {
$questions['questions'][$i]['question'] = $line;
} else {
$answer = [
'id' => rand(1596805341210, 9999999999999),
'marks' => null,
'value' => $line,
'selected' => false,
];
if (preg_match("/(\*)(.)+/", $line)) {
$line= str_replace('*','',$line);
$answer['value'] = $line;
$answer['marks'] = 100;
$answer['selected'] = true;
}
$questions['questions'][$i]['answer_options'][] = $answer;
}
}
$data=array();
}
and the returned data like this
{
"questions": [
{
"id": 9556585048005,
"type": "Multiple Choice Single Answer",
"question": "how are you ?\n",
"answer_options": [
{
"id": 2463233296661,
"marks": 100,
"value": "Iam good \n",
"selected": true
},
{
"id": 2918235956978,
"marks": null,
"value": "not good \n",
"selected": false
}
]
},
{
"id": 3438800692307,
"type": "Multiple Choice Single Answer",
"question": "what is your name?\n",
"answer_options": [
{
"id": 3338189982867,
"marks": null,
"value": "muhaammed\n",
"selected": false
},
{
"id": 9435683201111,
"marks": 100,
"value": "ahmed\n",
"selected": true
}
]
}
]
}
I want to do same things using word file, to get images from the word file if it contains it. I found phpword package for library, but to be honest I can not find the right way to use it. thank you in advance