Primitive filters
Posted: Sat Nov 08, 2008 5:48 pm
Because filtering is somewhat important to get right I'd like a quick review of the regex, etc and hopefully any errors are spotted. In addition maybe I missed a simple filter which you might then recommend.
NOTE: These are meant to be primitives nothing really fancy, although I have considered using HTML_Purifier instead of strip_tags. The convention is Filter_X - with X being the characters to filter or remove.
I'm not sure I could consider encoding or escaping as a logical part of this collection of static classes. Something higher level like a Filter_Email is not really nessecary as I use a validator which parses the Email according to RFC standards and MUST match so filtering here would be redundant.
What I am intereted in though is maybe filtering Numerics and not just digits, for instance, is the number a hex value, in which case leading 0x might be allowed. Currency filters would not make sense as those data variables rely on locality as well, which is not part of end goal for this.
Here are my four trivial filters hitherto:
NOTE: These are meant to be primitives nothing really fancy, although I have considered using HTML_Purifier instead of strip_tags. The convention is Filter_X - with X being the characters to filter or remove.
I'm not sure I could consider encoding or escaping as a logical part of this collection of static classes. Something higher level like a Filter_Email is not really nessecary as I use a validator which parses the Email according to RFC standards and MUST match so filtering here would be redundant.
What I am intereted in though is maybe filtering Numerics and not just digits, for instance, is the number a hex value, in which case leading 0x might be allowed. Currency filters would not make sense as those data variables rely on locality as well, which is not part of end goal for this.
Here are my four trivial filters hitherto:
Code: Select all
class Filter_Alpha implements Filter_Interface{
public static function filterMe($value)
{
return preg_replace('/[^0-9\.\+\-]/', '', $value);
}
}
class Filter_Html implements Filter_Interface{
public static function filterMe($value, $safe_tags = null)
{
if(is_array($safe_tags)){
$safe_tags = array_map(create_function('$element', 'return "<".strtolower($element).">";'), $safe_tags);
$safe_tags = implode('', $safe_tags);
}
else{
$safe_tags = '';
}
return strip_tags($value, $safe_tags);
}
}
class Filter_Digit implements Filter_Interface{
public static function filterMe($value)
{
return preg_replace('/\d/', '', $value);
}
}
class Filter_Space implements Filter_Interface{
public static function filterMe($value)
{
return trim($value);
}
}