zoukankan      html  css  js  c++  java
  • PHP word导入题库

    public function index()
        {
            $url = public_path()."static/word.docx";
            $word_obj = new PhpWord();
            $sections = IOFactory::load($url)->getSections();
            $arr = [];
            foreach ($sections as $key => $val){
                $elements = $val->getElements();
                foreach ($elements as $k => $v){
                    if($v instanceof TextRun){
                        $str = "";
                        $content_elements = $v->getElements();
                        foreach ($content_elements as $k1 => $v1){
                            if($v1 instanceof Text){
                                mb_convert_encoding($v1->getText(), 'UTF-8');
                                $str .=  $v1->getText();
    
                            }elseif ($v1 instanceof Image){
                                $imageDataTmp = $v1->getImageStringData(true);
                                $imageType = $v1->getImageType() ? $v1->getImageType() : 'image/jpg';
                                $imageData = 'data:' . $imageType . ';base64,' . str_replace(array("
    ", "
    ", "
    "), "", $imageDataTmp);
                                $imageSrc = './static/uploads/' . md5($v1->getSource()) . '.' . $v1->getImageExtension();
                                file_put_contents($imageSrc,base64_decode(explode(',',$imageData)[1]));
                                $str .= '<img src="'. $imageSrc .'" style="100%;height:auto">';
                            }
                        }
                        //echo $str."
    ";
                        $first_str = mb_substr( $str, 0, 1, "UTF-8" );
                        if(preg_match("/^d*$/",$first_str)){ //是数字
                            $temp = [];
                            $temp['title'] = $str; //题干
                        }elseif (preg_match('/[a-zA-Z]/',$first_str)){ //是字母
                            $temp['options'][] = $str; //选项
                        }else{ //是中文
                            $two_str = mb_substr( $str, 0, 2, "UTF-8" );
                            switch ($two_str){
                                case "答案":
                                    $temp['aswer'] = $str;
                                    break;
                                case "难度":
                                    $temp['difficulty'] = $str;
                                    break;
                                case "解析":
                                    $temp['parse'] = $str;
                                    array_push($arr,$temp);
                            }
                        }
                    }
    
                }
            }
            print_r($arr);
        }

    必须按照模板规则

  • 相关阅读:
    oracle_使用udev绑定磁盘方法
    Android studio实现简单的CRUD
    Android Studio无法打印Logout日志
    迭代法求平方根
    实现Hibernate框架的CRUD
    Android Studio连接真机调试
    Java项目学习笔记(一)
    绝对路径${pageContext.request.contextPath}
    request、response的setCharacterEncoding与response的setContentType
    java中的@Override标签
  • 原文地址:https://www.cnblogs.com/finnlee/p/14633726.html
Copyright © 2011-2022 走看看