• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > 百度编辑器上传word文件转为html

百度编辑器上传word文件转为html

作者:站长图库 字体:[增加 减小] 来源:互联网 时间:2022-04-29

站长图库向大家介绍了百度编辑器,word文件转html等相关知识,希望对您有所帮助

对于很多网络编辑小白来讲,经常会遇到网站富媒体编辑器,比如百度编辑器,wangeditor编辑器等不会使用,但是word用的很溜的情况,这篇教程教大家二次开发百度编辑器上传word文件转为html的方法,希望对大家有所帮助!

首先:后台上传word转html到编辑器中,编辑器是使用的百度编辑器,先在页面合适的位置写个上传框

<button type="button" name="fileword" id="upload-fileword">上传word文件</button>

前端用的LayUI框架,不需要写提示框,图个方便,大家自行选择

<script>layui.use('upload', function () {    var upload = 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) {    return json(['status' => 2, 'html' => $html, 'msg' => '解析失败']);}else{    return json(['status' => 1, 'html' => $html, 'msg' => '解析成功']);}

WordUpload文件 需要phpword包 自己下载

<?phpnamespace tool;use PhpOffice\PhpWord\PhpWord;use PhpOffice\PhpWord\IOFactory;use PhpOffice\PhpWord\Style\Font;use PhpOffice\PhpWord\Shared\ZipArchive;use PhpOffice\PhpWord\Settings;use PhpOffice\PhpWord\Shared\Converter;use PhpOffice\PhpWord\Style\TablePosition;use think\facade\Env;use think\facade\Config;use think\File;use think\Db; /*** Class WordUpload* [url=home.php?mod=space&uid=1507498]@Package[/url] app\common\util* word 文档上传*/class WordUpload{    private $file;    private $size;    private $ext;    private $savePath = '/upload/word/';    private $errorMsg;    private $delTmp = true;         /**    * WordUpload constructor.    * [url=home.php?mod=space&uid=952169]@Param[/url] $file [上传的文件]    * @param int $size [文件大小]    * @param string $ext [允许的后缀]    */    public function __construct($file, $size = 1024, $ext = 'docx')    {        $this->file = $file;        $this->size = $size;        $this->ext = $ext;    }    public function getHtml(){        $file = request()->file($this->file);        if (!$file instanceof File) {            $this->errorMsg = '请上传文件';            return false;        }        //上传文件 根据站点选择目录        $info = $file->validate(['size'=>$this->size,'ext'=>$this->ext])->move(Env::get('ROOT_PATH').'public/static'. $this->savePath);        if(!$info){            // 上传失败获取错误信息            $this->errorMsg = $file->getError();            return false;        }        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 == '') {                throw new \Exception('上传文件内容为空');            }            $html = $this->saveImage($html);            $html = $this->clearStyle($html);            $html = $this->clearSpan($html);        } catch (\Exception $e) {            $this->errorMsg = $e->getMessage();            return false;        }        return $html;    }    /**    * @param $html    * [url=home.php?mod=space&uid=155549]@Return[/url] string    * 匹配出body的内容    */    private function getHtmlBody($html) {        preg_match('/<body>([\s\S]*)<\/body>/', $html,$matches);        return isset($matches[1]) ? $matches[1] : '';    }    /**    * @param $html    * @return mixed    * 图片处理    */    private function saveImage($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);        }        return str_replace($imageMatches[1], $imageUrl, $html);    }    /**    * @param $base64_image_content    * @return string    * 保存图片到本地    */    private function saveLocalBase64Image($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    * @return null|string|string[]    * 清除p,span标签的样式    */    private function clearStyle($content)    {        $patterns = array (            '/<(p|span)[\s\S]*?>/i',        );        return preg_replace($patterns, '<$1>', $content);    }    /**    * @param $content    * @return null|string|string[]    * 清除span标签    */    private function clearSpan($content)    {        $patterns = array (            '/<span[\s\S]*?>/i',            '/<\/span>/i',        );        return preg_replace($patterns, '', $content);    }    /**    * @return mixed    */    public function getErrorMsg()    {        return $this->errorMsg;    }}


分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 百度编辑器上传word文件转为html
  • 百度编辑器无法插入视频、iframe怎么办

相关文章

  • 2022-04-29PHPcms远程图片本地化增加图片类型和后缀的方法
  • 2022-04-29讲解Laravel8.5是如何添加验证码mews/captcha的
  • 2022-04-29Photoshop快速制作创意的木块字
  • 2022-04-29Illustrator绘制超酷效果的立体字教程
  • 2022-04-29使用PHP中3个神奇常量快速获取目录、文件名和行号
  • 2022-04-29dedecms网站列表调用文章或图集的第一张图片原图
  • 2022-04-29Photoshop制作一个古典的小木箱
  • 2022-04-29Photoshop设计大气时尚的金色花纹教程
  • 2022-04-29Illustrator实例教程:抛光浮雕样式制作立体字
  • 2022-04-29PhotoShop制作漂亮的渐变星光文字效果教程

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • 微信小程序wx.request请求数据报错
    • javascript如何删除数组里的某个元素
    • 说说在Laravel中怎么执行Shell命令 ?
    • Photoshop调出梦幻炫彩的菱形背景图
    • php使用blob存取图片的信息(含源码)
    • Photoshop制作金属质感的工具图标
    • 分享几种实用的Node.js调试方法,快来收藏吧!!
    • 关于ThinkPHP的join关联查询不使用默认的表前缀
    • 解决PHP SWOOLEC loader ext not installed方法教程
    • 为了使用邮箱服务,php7该如何配置sendmail

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有