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

时间:2015-11-13 12:37:30

标签: php yii ckeditor

Ckeditor中使用Yii扩展我只想在数据库中插入数据,

一旦Ckeditor显示其正常但在Db中插入带<p>标记的数据。

例如,

<p>Sample</p>

在Db中插入

但我只想Sample而不是<p><br>标签等。

1 个答案:

答案 0 :(得分:0)

您可以使用PHP strip_tags来删除控制器中的html标记(请注意这将删除所有html标记),例如:

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,
        ));
    }

修改

如果要删除显示的默认<p>,可以执行以下操作:

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

以下是完整的步骤:

  • Download Ckeditor。
  • 提取下载的文件并将ckeditor文件夹放在protected文件夹之外。您的文件夹结构应类似于:webapp/ckeditor
  • 在视图中添加代码:

    <?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>