thinkphp5 整合wangEditor3 可用版

本文分享了使用wangEditor3富文本编辑器的实际经验,解决了文本编辑和图片上传的问题,提供了前端代码示例及PHP后端实现,帮助开发者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要做个东西,wangEditor3 的文本不怎么详细,而且网上文章也很乱,耽误了我两天时间,昨晚搞定了,今天放出来让用到的小伙伴使用

先贴前端代码

<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="__PUBLIC__/css/font.css">
    <link rel="stylesheet" href="__PUBLIC__/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="__PUBLIC__/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="__PUBLIC__/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询,从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-body">
        <form class="layui-form" action='groupAdd' method="POST" enctype="multipart/form-data" >
         
          <div class="layui-form-item layui-form-text">
             <label for="desc" class="layui-form-label">
                  <span class="x-red"></span>
              </label>
              <div>
                  <span class="x-red">*</span>注:图片限制为5张,图片大小不超过5M,图片不能不能为中文</span>
              </div>
          </div>
           <div class="layui-form-item layui-form-text">
              <label for="desc" class="layui-form-label">
                  <span class="x-red">*</span>路线详情
              </label>
              <div id="editor" style="width:92%; height:400px;margin-left:110px;">
                 <p></p>
              </div>
                
               <textarea name="lineContent" id="lineContent" style="display:none;"></textarea>
          </div>


          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
              </label>
              <button  type="submit" class="layui-btn" lay-filter="add" lay-submit="">
                  增加
              </button>
          </div>
      </form>
    </div>
    <!-- 注意, 只需要引用 JS,无需引用任何 CSS !!!-->
    <script type="text/javascript" src="__PUBLIC__/release/wangEditor.min.js"></script>
    <script type="text/javascript">
        var E = window.wangEditor
        var editor = new E('#editor')
        // 或者 var editor = new E( document.getElementById('editor') )
        // 
        // 关闭粘贴样式的过滤  //官网给的不起作用
        editor.customConfig.pasteFilterStyle = false   
        // 忽略粘贴内容中的图片
        editor.customConfig.pasteIgnoreImg = true

        var $lineContent = $('#lineContent')
        editor.customConfig.onchange = function (html) {
            // 监控变化,同步更新到 textarea
            $lineContent.val(html)
        }

        //绿色部分是对图片的设置,本地上传
        editor.customConfig.debug = true;
        editor.customConfig.uploadFileName = 'image';                       //上传的文件名
        // 或者 var editor = new E( document.getElementById('editor') )
        editor.customConfig.uploadImgServer = "uploadimg";  // 上传图片到服务器
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024;         //图片的尺寸
        editor.customConfig.uploadImgMaxLength = 5;                     //最多上传多少
        editor.customConfig.uploadImgParams = {
            type: 'editor'  // 属性值会自动进行 encode ,此处无需 encode
        };
        
        // 自定义处理粘贴的文本内容
        this.editor.customConfig.pasteTextHandle = function (lineContent) {
          // content 即粘贴过来的内容(html 或 纯文本),可进行自定义处理然后返回
          if (lineContent == '' && !lineContent) return ''
          var str = lineContent
          str = str.replace(/<xml>[\s\S]*?<\/xml>/ig, '')
          str = str.replace(/<style>[\s\S]*?<\/style>/ig, '')
          str = str.replace(/<\/?[^>]*>/g, '')
          str = str.replace(/[ | ]*\n/g, '\n')
          str = str.replace(/&nbsp;/ig, '')
          return str
        }

        editor.create()
        // 初始化 textarea 的值
        $lineContent.val(editor.txt.html())
    </script>
  </body>

</html>

下面上PHP的后端代码

 public function groupAdd(){

    $data =input('post.');
    Db::table('xxxxx')->insert($data)
}


//上传富文本图片函数
    public function uploadimg(){
        // 获取表单上传的文件,例如上传了一张图片
        $file = request()->file('image');
        if($file){
            //将传入的图片移动到框架应用根目录/public/uploads/editorimg 目录下,ROOT_PATH是根目录下,DS是代表斜杠 / 
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'. DS . 'editorimg');
            if ($info) {
            	  $img_info = str_replace('\\', '/',$info->getSaveName());
                  $url   = SITE_URL_IMG."/public/uploads/editorimg/".$img_info ;
                  $datas = ["errno" => 0, "data" => [$url]];
                  return json($datas);
                  } else {
                   // 上传失败获取错误信息
                   echo $file->getError();
               }
        }
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值