<?php
class Sanitizer {
public static function email($email){
return filter_var($email, FILTER_SANITIZE_EMAIL);
}
public static function alfabetico($valor, bool $allow_accents = true, bool $allow_spaces = false){
$valor = str_replace(array('"', "'", '`', '′', '¨'), '', trim($valor));
if(!$allow_accents && !$allow_spaces){
return preg_replace('#[^A-Za-z]#', '', $valor);
}
if($allow_accents && !$allow_spaces){
return preg_replace('#[^A-Za-zà-?à-?]#', '', $valor);
}
if(!$allow_accents && $allow_spaces){
return preg_replace('#[^A-Za-z ]#', '', $valor);
}
if($allow_accents && $allow_spaces){
return preg_replace('#[^A-Za-zà-?à-? ]#', '', $valor);
}
}
public static function alfanumerico($valor, bool $allow_accents = true, bool $allow_spaces = false){
$valor = str_replace(array('"', "'", '`', '′', '¨'), '', trim($valor));
if(!$allow_accents && !$allow_spaces){
return preg_replace('#[^A-Za-z0-9]#', '', $valor);
}
if($allow_accents && !$allow_spaces){
return preg_replace('#[^A-Za-zà-?à-?0-9]#', '', $valor);
}
if(!$allow_accents && $allow_spaces){
return preg_replace('#[^A-Za-z0-9 ]#', '', $valor);
}
if($allow_accents && $allow_spaces){
return preg_replace('#[^A-Za-zà-?à-?0-9 ]#', '', $valor);
}
}
public static function numerico($valor){
return preg_replace('/\D/', '', $valor);
}
public static function integer($valor){
return (int)$valor;
}
public static function float($valor){
return (float)$valor;
}
public static function money($valor){
$valor = preg_replace('/\D/', '', $valor);
if(strlen($valor) < 3){
$valor = substr($valor, 0, strlen($valor)).'.00';
return (float)$valor;
}
if(strlen($valor) > 2){
$valor = substr($valor, 0, (strlen($valor)-2)).'.'.substr($valor, (strlen($valor)-2));
return (float)$valor;
}
}
public static function url($valor){
$valor = strip_tags(str_replace(array('"', "'", '`', '′', '¨'), '', trim($valor)));
return filter_var($valor, FILTER_SANITIZE_URL);
}
}
var_dump(Sanitizer::numerico('bgusybd458ad8964sdfsd'));
?>