Home>

1. Use in the controller:

public function actioncreate ()
{
  $model=new news;
  $purifier=new chtmlpurifier ();
  $purifier->options=array (
    "uri.allowedschemes" =>array (
              "http" =>true,              "https" =>true,    ),       "html.allowed" =>"div",  );
  if (isset ($_ post ["news"]))
  {
    $model->attributes=$_ post ["news"];
    $model->attributes ["content"]=$purifier->purify ($model->attributes ["content"]);
    if ($model->save ())
      $this->redirect (array ("view", "id" =>$model-&id;id));
  }
}

2. Use in models:

protected function beforesave ()
{
  $purifier=new chtmlpurifier ();
  $purifier->options=array (
    "uri.allowedschemes" =>array (
              "http" =>true,              "https" =>true,    ),       "html.allowed" =>"div",  );
  if (parent ::beforesave ()) {
    if ($this->isnewrecord) {
      $this->create_data=date ("y-m-d h:m:s");
      $this->content=$purifier->purify ($this->content);
    }
    return true;
  } else {
    return false;
  }
}

3. Use in filters:

public function filters ()
{
  return array (
    "accesscontrol", //perform access control for crud operations
    "postonly + delete", //we only allow deletion via post request
    "purifier + create", //perform some filtering operations when loading the inserted page
  );
}
public function filterpurifier ($filterchain) {
  $purifier=new chtmlpurifier ();
  $purifier->options=array (
    "uri.allowedschemes" =>array (
              "http" =>true,              "https" =>true,    ),       "html.allowed" =>"div",  );
  if (isset ($_ post ["news"]) {
    $_post ["news"] ["content"]=$purify ($_ post ["news"] ["content"]);
  }
    $filterchain->run ();
}

4. Use in views:

<?php $this->beginwidget ("chtmlpurifier");?>
... display user-entered content here ...
<?php $this->endwidget ();?>
php
  • Previous Android code to get information such as the phone's version number
  • Next Yii CActiveForm-based Ajax data validation usage example