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>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog