I'm sure I'm missing something silly, but I cannot get this Behavior
to work. I've got data with a bunch of nasty characters in the
database that I need to avoid or escape before presenting to the
browser. Also, it's got a lot of white space at the end of much of the
data because someone erroneously used the nchar datatype on the MS-SQL
database I'm working off of. So this Behavior is just to be able to
get rid of those problems going forward. Right now, I'm using it on
the afterFind hook, but once I know it works safely I'll apply it to
the beforeSave hook:

<?php
App::import('Sanitize');
class TrimAndEscapeBehavior extends ModelBehavior {
        var $errors             = array();

        function afterFind(&$Model, $results, $primary) {
                $results = $this->cleanText($results);
                Sanitize::clean($results);
                return $results;
        }


        /*
        // cleanText:                   Recursive function trims text and 
removes illegal
characters.
        // @var mixed $data:    The data to inspect and clean.
        // @data mixed $data:   The cleaned data.
        */
        function cleanText($results) {
                // If this is an array, send it back through:
                if(is_array($results)) :
                        foreach($results as $key=>$value) :
                                $results[$key]  = $this->cleanText($value);
                        endforeach;
                // We have a text node. Trim and escape:
                else :
                        // replace Trademarks:
                        $results = str_replace(chr(8482), '<sup>&trade;</sup>', 
$results);
                        // Replace Registered Trademarks
                        $results = str_replace(chr(174), '<sup>&reg;</sup>', 
$results);
                        // replace Trademarks:
                        $results = str_replace('&#8482;', '<sup>&trade;</sup>', 
$results);
                        // Replace Registered Trademarks
                        $results = str_replace('&#174;', '<sup>&reg;</sup>', 
$results);
                        // Trim content:
                        $results = trim($results);
                        return $results;
                endif;
        }
}
?>

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to