欢迎光临 - 我的站长站,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!

php教程

百度编辑器上传word转html

php教程 我的站长站 2021-10-22 共78人阅读

后台上传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 toolWordUpload(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包 自己下载

<?php
namespace tool;
use PhpOfficePhpWordPhpWord;
use PhpOfficePhpWordIOFactory;
use PhpOfficePhpWordStyleFont;
use PhpOfficePhpWordSharedZipArchive;
use PhpOfficePhpWordSettings;
use PhpOfficePhpWordSharedConverter;
use PhpOfficePhpWordStyleTablePosition;
use thinkfacadeEnv;
use thinkfacadeConfig;
use thinkFile;
use thinkDb;
/**
* Class WordUpload
* [url=home.php?mod=space&uid=1507498]@Package[/url] appcommonutil
* 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>([sS]*)</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="([sS]*?)"/>/', $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)[sS]*?>/i',
);
return preg_replace($patterns, '<$1>', $content);
}
/**
* @param $content
* @return null|string|string[]
* 清除span标签
*/
private function clearSpan($content)
{
$patterns = array (
'/<span[sS]*?>/i',
'/</span>/i',
);
return preg_replace($patterns, '', $content);
}
/**
* @return mixed
*/
public function getErrorMsg()
{
return $this->errorMsg;
}
}


相关专题
编辑器
编辑器
2021-05-14 209

编辑器是网站开发必备插件,编辑器专题为大家整理各类型编辑器下载,包含热门的CMS程序编辑器插件下载,html编辑器源码下载等等....

相关推荐
  • 百度编辑器
  • php上传
  • 帝国cms7.5整合百度ueditor编辑器
    帝国cms7.5整合百度ueditor编辑器

    完美集成了帝国cms,不需要修改帝国cms核心代码。加入了管理验证功能。集成步骤简单。帝国cms7.5整合百度ueditor编辑器步骤1.下载定制好的百度插件,下载后解压到e/extend/lskue目录下即可2.在后台...

    帝国cms插件 1749 5年前
  • 帝国CMS7.2百度编辑器修改版插件
    帝国CMS7.2百度编辑器修改版插件

    为什么要介绍这款帝国百度编辑器了?因为站长在以往更新网站时遇到一个问题,就是按照网上方法安装的百度编辑器有个问题,就是贴入的PHP,JS代码有时候不能保存。类是这种方法安装的帝国CMS百度编辑器:h...

    帝国cms插件 2007 6年前
  • 帝国CMS百度编辑器一键排版插件
    帝国CMS百度编辑器一键排版插件

    当我们需要借鉴其他网站内容时候,最苦恼的就是花上大把时间去编辑复制过来的内容代码。需要逐个清楚内容代码中不需要的代码,虽然大部分编辑器都有清除辣鸡代码的功能,但有时候并不能满足我们自身的...

    帝国cms插件 2388 6年前
  • 帝国CMS7.5整合ueditor 1.4.3百度编辑器教程

    帝国CMS7.5终于更新了自带的后台编辑器,但功能还是太少且很多地方有问题,所以很多人还是愿意用帝国CMS7.5整合了ueditor 1.4.3百度编辑器 UTF-8版本,下面将教程和自己修改优化过的百度编辑器ueditor 1.4.3分享给大家!帝国CMS7.5整合ueditor 1.4.3百度编...

    帝国cms教程 261 5年前
  • 帝国cms7.0整合百度编辑器ueditor教程
    帝国cms7.0整合百度编辑器ueditor教程

    帝国cms7.0整合百度编辑器ueditor教程开始1、根据自己使用的帝国cms版本编码下载对应的ueditor版本下载地址 http://ueditor.baidu.com/website/download.html#ueditor 2、解压附件,重命名为”ued...

    帝国cms教程 1418 9年前
  • php上传大文件必备配置方法

    项目要求如果你的项目需要用到大文件上传或下载功能,就必须首先修改PHP的配置才行,否则上传或下载操作就会超时,操作失败。操作步骤打开php配置文件php.ini,首先找到; file uploads ;区域,有影响文件上传的以下几个参数:file_uploads = on ;//是否允许...

    php教程 52 1年前
  • 百度编辑器上传word转html

    后台上传word转html到编辑器中,编辑器是使用的百度编辑器,先在页面合适的位置写个上传框<button type=”button” name=”fileword” id=”upload-fileword”>上传word文件</button>前端用的layui框架,不需要写提示框,图个方便,大家自行选择<script>layui...

    php教程 78 2年前
  • jQuery+PHP实现简单的图片上传功能

    图片上传思路:通过ajax实现图片上传,然后把PHP返回的图片地址,加入到隐藏字段中,最后通过表单提交给后台PHP,代码如下HTML代码 zimg.html文件:<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, init...

    php教程 77 3年前
  • PHP新手必学文件上传下载实例

    都知道PHP无非就是增删改查,学会了增删改查就入门了PHP,我的站长站分享一篇PHP新手必学文件上传下载实例,快来学习吧。主页index.php<html><head><title>图片操作</title><style>#contains {width: 500px;margin: 0 auto;text-align: center;color: #0F...

    php教程 45 3年前
  • 京东图床上传api接口源码

    API接口源码<?phpif (class_exists(&#39;CURLFile&#39;)) { // php 5.5$post[&#39;file&#39;] = new \CURLFile(realpath($_FILES[&#39;Filedata&#39;][&#39;tmp_name&#39;]));} else {$post[&#39;file&#39;] = &#39;@&#39;....

    php教程 148 3年前