规定word导入格式
1、[单选题][2024][一般]题目1
A.选项1
B.选项2
C.选项3
D.选项4
答案:D
试题图片(上传多媒体图片):
分数:2
答案解析:
2、[多选题][2024][困难]题目2
A.选项1
B.选项2
C.选项3
D.选项4
E.选项5
答案:AE
分数:2
答案解析:
3、[填空题][2024][一般]题目3
答案:答案1、答案2
分数:3
答案解析:
4、[简答题][2017][一般]题目4
答案:答案内容
分数:5
设置关键词:关键词1、关键词2
答案解析:
PHP获取word内容并导入数据库的代码:
<?phppublic function import_word() {$word = "question.docx";$phpWord = \PhpOffice\PhpWord\IOFactory::load($word, 'Word2007', 'utf-8');$sections = $phpWord->getSections();$wordData = [];//将内容分句子foreach ($sections as $section) {$elements = $section->getElements();foreach ($elements as $element) {if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {$wordData[] = $element->getText();foreach($element->getElements() as $text){//导入每道题的图片if ($text instanceof \PhpOffice\PhpWord\Element\Image) {$file_path = './uploads/images'.uniqid().'.jpg';file_put_contents($file_path, trim($text->getImageString()));$wordData[] = 'image:'.Config::get('web_site_domain').ltrim($file_path, '.');} }}}}//拆分句子$questionData = [];$sectionData = [];foreach($wordData as $val){if($val){if(strpos($val, "[单选题]") !== false){if ($sectionData) {$questionData[] = $sectionData;$sectionData = [];}}else if(strpos($val, "[多选题]") !== false){if ($sectionData) {$questionData[] = $sectionData;$sectionData = [];}}else if(strpos($val, "[填空题]") !== false){if ($sectionData) {$questionData[] = $sectionData;$sectionData = [];}}else if(strpos($val, "[简答题]") !== false){if ($sectionData) {$questionData[] = $sectionData;$sectionData = [];}}$sectionData[] = $val;}}if($sectionData){$questionData[] = $sectionData;}//对试题数组过滤数据$questions = [];if ($questionData) {foreach ($questionData as $key1=>$val1) {$title = "";$answer = "";$options = "";$score = 0;$question_type = 0;$year = 0;$difficulty = 0;$question_keywords = "";$answer_key = "";$title = trim($val1[0]);$index1 = strpos($title, '[');$title = substr($title, $index1);if (strpos($title, "[单选题]") === 0) {$question_type = 1;$title = str_replace("[单选题]", "", $title);} else if (strpos($title, "[多选题]") === 0) {$question_type = 2;$title = str_replace("[多选题]", "", $title);} else if (strpos($title, "[填空题]") === 0) {$question_type = 3;$title = str_replace("[填空题]", "", $title);} else if (strpos($title, "[简答题]") === 0) {$question_type = 4;$title = str_replace("[简答题]", "", $title);}preg_match_all("/\[\d+\]/", $title, $matchs);if (isset($matchs[0][0]) && $matchs[0][0]) {$year = str_replace(["[", "]"], "", $matchs[0][0]);$title = str_replace("[{$year}]", "", $title);} else {$title = str_replace("[]", "", $title);}if (strpos($title, "[容易]") === 0) {$difficulty = 1;$title = str_replace("[容易]", "", $title);} else if (strpos($title, "[一般]") === 0) {$difficulty = 2;$title = str_replace("[一般]", "", $title);} else if (strpos($title, "[困难]") === 0) {$difficulty = 3;$title = str_replace("[困难]", "", $title);} else {$title = str_replace("[]", "", $title);}unset($val1[0]);$optionData = [];$images = [];foreach ($val1 as $k1 => $v1) {for ($i = 0; $i < 26; $i++) {$option_id = chr($i + 65).".";if (strpos($v1, $option_id) === 0) {$optionData[] = str_replace($option_id, "", $v1);}}if(strpos($v1, "image:") === 0) {$images[] = str_replace("image:", "", $v1);}if (strpos($v1, "答案:") === 0) {$answer = str_replace("答案:", "", $v1);}if (strpos($v1, "分数:") === 0) {$score = str_replace("分数:", "", $v1);}if (strpos($v1, "答案解析:") === 0) {$answer_key = str_replace("答案解析:", "", $v1);}if (strpos($v1, "设置关键词:") === 0) {$question_keywords = str_replace("设置关键词:", "", $v1);}}$question_num = $key1 + 1;//判断每道题是否符合规则if(!$title){$this->error("第".$question_num."道题【标题有误】");}if(!$question_type){$this->error("第".$question_num."道题【题型有误】");}if(!$answer){$this->error("第".$question_num."道题【答案有误】");}if(!$difficulty){$this->error("第".$question_num."道题【难度有误】");}if (in_array(trim($question_type), [1, 2])) {if(!$optionData){$this->error("第".$question_num."道题【选项有误】");}$options = [];foreach ($optionData as $k2 => $v2) {$options[$k2]['options_title'] = $v2;$options[$k2]['options_value'] = 0;}$options = FreelistoftestquestionsModel::validateAnswer($options, trim($answer));if(!$options){$this->error("第".$question_num."道题【选项有误】");}}$questions[] = ['title' => trim($title),'options' => $options,'answer' => trim($answer),'images' => implode(',', $images),'question_type' => $question_type,'year' => trim($year),'score' => intval($score),'question_keywords' => trim($question_keywords),'difficulty' => $difficulty,'answer_key' => trim($answer_key),'status' => 1,'create_time' => time(),];}}if ($questions) {if (!QuestionsModel::insertAll($questions)) {return false;}return true;} else {return false;}}