Yii Framework Forum: [Solved] Filtering Bad Words At Page Level (Not At Model Level) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Solved] Filtering Bad Words At Page Level (Not At Model Level) Using filters Rate Topic: -----

#1 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

  Posted 04 September 2013 - 07:12 AM

Hi, I need to filter text from pages to discard "bad words". I found this:

http://www.yiiframew...ad-words-filter

But I don't want to modify all my models. I'd like to do it modifying the controllers to use a filter to catch the text, remove the unwanted text and then continue rendering. But I can't figure out how or where to get the buffer or text to render (unless I create a controller class which overrides render and renderPartial to do filtering there? I'm a bit lost here :unsure:) Any hint is appreciated. Thanks.
Ah! on-off, simplement!
0

#2 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 820
  • Joined: 02-July 10
  • Location:Central Poland

Posted 04 September 2013 - 08:20 AM

you can use widget with beginWidget/endWidget scenario and put filtering of any text rendered between beginWidget/endWidget in run() method.

you can achieve it by putting "ob_start()" in widget init() method, and process it in run() method:
class MyWidget extends CWidget
{
    public function init()
    {
        ob_start();
    }
 
    public function run()
    {
        $content = ob_get_content();
        ob_end_clean();
        echo preg_replace( bad_word_pattern, replcatement, $content );
    }
}


and in view:
<?php $this->beginWidget( 'MyWidget', array( params ... ) ); ?>

generate any content here, model attributes, etc
this content will be captured and processed by widget

<?php $this->endWidget(); ?>

red
0

#3 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 04 September 2013 - 03:42 PM

View Postredguy, on 04 September 2013 - 08:20 AM, said:

you can use widget with beginWidget/endWidget scenario and put filtering of any text rendered between beginWidget/endWidget in run() method.


That would be one way, thanks redguy. What about doing it at controller level? Do you think it's possible?
Ah! on-off, simplement!
0

#4 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 820
  • Joined: 02-July 10
  • Location:Central Poland

Posted 05 September 2013 - 06:45 AM

View PostMetaYii, on 04 September 2013 - 03:42 PM, said:

That would be one way, thanks redguy. What about doing it at controller level? Do you think it's possible?

you could use filter to do that (write filter class with postprocessing and define it in controllers filters() function), but with widget you can control which parts should be censored and which not...
red
0

#5 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 09 September 2013 - 07:43 PM

Solved, this is a sample code, just replace the contets of arrays $bad and $good with the original text and the replacement:

class Charset3Filter extends COutputProcessor
{
   public function processOutput($output)
   {
  	$bad  = array("á", "é", "í", "ó", "ú", "ñ", "Á", "É", "Í", "Ó", "Ú", "Ã");
  	$good = array('á', 'é', 'í', 'ó', 'ú', 'Á', 'É', 'Í', 'Ó', 'Ú', 'ñ', 'Ñ');

  	$output = str_replace($bad, $good, $output);
  	parent::processOutput($output);
   }
}


And use it as a normal filter in your controller's filter() function:

array(
          	'application.filters.Charset3Filter',
      	),

Ah! on-off, simplement!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users