doukanmang3687 2015-11-13 12:37
浏览 35
已采纳

yii中的Ckeditor仅在DB中插入数据而不在html标记中插入数据

Using Ckeditor extension in Yii I want to insert data only in Database,

Once Ckeditor Showing its fine but insert data in Db with <p> tag itself.

For example,

<p>Sample</p>

Insert in Db

But I want Sample only and not the <p> and <br> tags etc..

  • 写回答

1条回答 默认 最新

  • duanji1902 2015-11-13 12:46
    关注

    You can use PHP strip_tags for this and strip out html tags in your controller (note that this will remove all the html tags), for example:

    public function actionCreate()
        {
            $model=new MyModel;
    
            if(isset($_POST['MyModel']))
            {
                $model->attributes=$_POST['MyModel'];
                $model->mytext = strip_tags($model->mytext);//strip html tags
                if($model->save())
                    $this->redirect(array('view','id'=>$model->id));
            }
    
            $this->render('create',array(
                'model'=>$model,
            ));
        }
    

    EDIT:

    If you want to remove the default <p> that appears, you can do the following :

    var config = {
    ...
    enterMode : CKEDITOR.ENTER_BR,
    shiftEnterMode: CKEDITOR.ENTER_P,
    ....
    };
    

    Here are the complete steps to follow:

    • Download Ckeditor.
    • Extract the downloaded file and place the ckeditor folder outside protected folder. Your folder structure should be like: webapp/ckeditor
    • Add the code in your view:

      <?php
      $js=Yii::app()->getClientScript();
      $js->registerScriptFile(Yii::app()->baseUrl.'/ckeditor/ckeditor.js');
      $js->registerScriptFile(Yii::app()->baseUrl.'/ckeditor/adapters/jquery.js');
      
      $js->registerScript(
        'js2',
        '
          var config = {
          toolbar:
          [
           ["Bold", "Italic","Underline", "-", "NumberedList", "BulletedList", "-" ],  
           ["UIColor"],["TextColor"],["Undo","Redo","Link"],
           ["JustifyLeft","JustifyCenter","JustifyRight","JustifyBlock"],
           ["NumberedList","BulletedList","FontSize","Font","Preview"]
          ],
          enterMode : CKEDITOR.ENTER_BR,
          shiftEnterMode: CKEDITOR.ENTER_P,
          height:150,
          width:580
          };
          $("#State_state").ckeditor(config);
        ',
        CClientScript::POS_LOAD
      );
      ?>
      <div class="form">
      
      <?php $form=$this->beginWidget('CActiveForm', array(
          'id'=>'state-form',
          'enableAjaxValidation'=>false,
      )); 
      ?>
      
      <div class="row">
          <?php echo $form->labelEx($model,'state'); ?>
          <?php echo $form->textArea($model,'state'); ?>
          <?php echo $form->error($model,'state'); ?>
      </div>
      
      
      <div class="row buttons">
          <?php echo CHtml::submitButton('Save'); ?>
      </div>
      
      <?php $this->endWidget(); ?>
      </div>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集