对于很多网络编辑小白来讲,经常会遇到网站富媒体编辑器,比如百度编辑器,wangeditor编辑器等不会使用,但是word用的很溜的情况,这篇教程教大家二次开发百度编辑器上传word文件转为html的方法,希望对大家有所帮助!
首先:后台上传word转html到编辑器中,编辑器是使用的百度编辑器,先在页面合适的位置写个上传框
<buttontype="button"name="fileword"id="upload-fileword">上传word文件</button>
前端用的LayUI框架,不需要写提示框,图个方便,大家自行选择
<script>layui.use('upload',function(){varupload=layui.upload;upload.render({elem:'#upload-fileword',accept:'file',url:'".url('upload/uploadword',['file'=>'file'])."',exts:'docx',done:function(data){if(data.status==1){UE.getEditor('".$field."').setContent(data.html);layer.msg(data.msg,{icon:1,time:1500,shade:0.1,});}else{layer.msg(data.msg,{icon:2,time:1500,shade:0.1,});}}});});</script>
php上传代码
$upload=new\\tool\\WordUpload(input('file'));$html=$upload->getHtml();if($html===false){returnjson(['status'=>2,'html'=>$html,'msg'=>'解析失败']);}else{returnjson(['status'=>1,'html'=>$html,'msg'=>'解析成功']);}
WordUpload文件 需要phpword包 自己下载
<?phpnamespacetool;usePhpOffice\\PhpWord\\PhpWord;usePhpOffice\\PhpWord\\IOFactory;usePhpOffice\\PhpWord\\Style\\Font;usePhpOffice\\PhpWord\\Shared\\ZipArchive;usePhpOffice\\PhpWord\\Settings;usePhpOffice\\PhpWord\\Shared\\Converter;usePhpOffice\\PhpWord\\Style\\TablePosition;usethink\\facade\\Env;usethink\\facade\\Config;usethink\\File;usethink\\Db;/***ClassWordUpload*[url=home.php?mod=space&uid=1507498]@Package[/url]app\\common\\util*word文档上传*/classWordUpload{private$file;private$size;private$ext;private$savePath='/upload/word/';private$errorMsg;private$delTmp=true;/***WordUploadconstructor.*[url=home.php?mod=space&uid=952169]@Param[/url]$file[上传的文件]*@paramint$size[文件大小]*@paramstring$ext[允许的后缀]*/publicfunction__construct($file,$size=1024,$ext='docx'){$this->file=$file;$this->size=$size;$this->ext=$ext;}publicfunctiongetHtml(){$file=request()->file($this->file);if(!$fileinstanceofFile){$this->errorMsg='请上传文件';returnfalse;}//上传文件根据站点选择目录$info=$file->validate(['size'=>$this->size,'ext'=>$this->ext])->move(Env::get('ROOT_PATH').'public/static'.$this->savePath);if(!$info){//上传失败获取错误信息$this->errorMsg=$file->getError();returnfalse;}try{//获取文件路径$tempDocx=Env::get('ROOT_PATH').'public/static'.$this->savePath.$info->getSaveName();$objWriter=IOFactory::createWriter(IOFactory::load($tempDocx),'HTML');$tempHtml=Env::get('ROOT_PATH').'public/static'.$this->savePath.substr($info->getSaveName(),0,strpos($info->getSaveName(),'.')).'.html';$objWriter->save($tempHtml);$html=file_get_contents($tempHtml);if($this->delTmp){//删除临时文件register_shutdown_function(function()use($tempHtml,$tempDocx){unlink($tempHtml);unlink($tempDocx);});}$html=$this->getHtmlBody($html);if($html==''){thrownew\\Exception('上传文件内容为空');}$html=$this->saveImage($html);$html=$this->clearStyle($html);$html=$this->clearSpan($html);}catch(\\Exception$e){$this->errorMsg=$e->getMessage();returnfalse;}return$html;}/***@param$html*[url=home.php?mod=space&uid=155549]@Return[/url]string*匹配出body的内容*/privatefunctiongetHtmlBody($html){preg_match('/<body>([\\s\\S]*)<\\/body>/',$html,$matches);returnisset($matches[1])?$matches[1]:'';}/***@param$html*@returnmixed*图片处理*/privatefunctionsaveImage($html){//匹配图片ini_set('pcre.backtrack_limit',9999999);preg_match_all('/<img[^>]*src="([\\s\\S]*?)"\\/>/',$html,$imageMatches);if(!$imageMatches[1]){return$html;}//print_r($imageMatches[1]);exit;$imageUrl=[];foreach($imageMatches[1]as$image){$imageUrl[]=$this->saveLocalBase64Image($image);}returnstr_replace($imageMatches[1],$imageUrl,$html);}/***@param$base64_image_content*@returnstring*保存图片到本地*/privatefunctionsaveLocalBase64Image($base64_image_content){$imge_web_url='';if(preg_match('/^(data:\\s*image\\/(\\w+);base64,)/',$base64_image_content,$result)){//图片后缀$type=$result[2];//保存位置--图片名$image_name=date('His').str_pad(mt_rand(1,99999),5,'0',STR_PAD_LEFT).'.'.$type;//图片名称$image_file_path=$this->savePath.'image/'.date('Ymd');//static/upload/word/image/20200522$image_file=Env::get('ROOT_PATH').'public/static'.$image_file_path;$imge_real_url=$image_file.DIRECTORY_SEPARATOR.$image_name;$imge_web_url='/static'.$image_file_path.DIRECTORY_SEPARATOR.$image_name;if(!file_exists($image_file)){mkdir($image_file,0700,true);}//解码$decode=base64_decode(str_replace($result[1],'',$base64_image_content));$res=file_put_contents($imge_real_url,$decode);return$imge_web_url;}return$imge_web_url;}/***@param$content*@returnnull|string|string[]*清除p,span标签的样式*/privatefunctionclearStyle($content){$patterns=array('/<(p|span)[\\s\\S]*?>/i',);returnpreg_replace($patterns,'<$1>',$content);}/***@param$content*@returnnull|string|string[]*清除span标签*/privatefunctionclearSpan($content){$patterns=array('/<span[\\s\\S]*?>/i','/<\\/span>/i',);returnpreg_replace($patterns,'',$content);}/***@returnmixed*/publicfunctiongetErrorMsg(){return$this->errorMsg;}}
产品猿社区致力收录更多优质的商业产品,给服务商以及软件采购客户提供更多优质的软件产品,帮助开发者变现来实现多方共赢;
日常运营的过程中我们难免会遇到各种版权纠纷等问题,如果您在社区内发现有您的产品未经您授权而被用户提供下载或使用,您可按照我们投诉流程处理,点我投诉;
本文来自用户发布投稿,不代表产品猿立场 ;若对此文有疑问或内容有严重错误,可联系平台客服反馈;
部分产品是用户投稿,可能本文没有提供官方下下载地址或教程,若您看到的内容没有下载入口,您可以在我们产品园商城搜索看开发者是否有发布商品;若您是开发者,也诚邀您入驻商城平台发布的产品,地址:点我进入;
如若转载,请注明出处:http://www.chanpinyuan.cn/42896.html;