[Solved] Filtering Bad Words At Page Level (Not At Model Level)

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


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.

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()





    public function run()


        $content = ob_get_content();


        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(); ?>

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…

Solved, this is a sample code, just replace the contets of arrays [font="Courier New"]$bad[/font] and [font="Courier New"]$good[/font] 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);




And use it as a normal filter in your controller’s [font=“Courier New”]filter()[/font] function: