Location: PHPKode > scripts > Janitor > janitor/janitor.class.php
<?php

/**
 *        Version: 1.0
 *    Description: Validate and/or santize data
 *   Dependencies: None
 *         Author: Kenth Hagström, www.designpalatset.se
 *   Requirements: PHP v5.2.0 or later
 *        License: http://creativecommons.org/licenses/by/3.0/
**/
class janitor
{
    public function __construct() { }
    
    /**
     *   @access public
     *   @param $string     string
     *   @return boolean
    **/
    public function validateAlpha( $string )
    {
        $options = array( 'options' => array( 'regexp' => "/^[a-zA-Z]+$/" ) );
        return filter_var( $string, FILTER_VALIDATE_REGEXP, $options );
    }
    
    /**
     *   @access public
     *   @param $string     string
     *   @return boolean
    **/
    public function validateAlphanum( $string )
    {
        $options = array( 'options' => array( 'regexp' => "/^[a-zA-Z0-9]+$/" ) );
        return filter_var( $string, FILTER_VALIDATE_REGEXP, $options );
    }
    
    /**
     *   @access public
     *   @param $email     string
     *   @return boolean
    **/
    public function validateEmail( $email )
    {
        $status = filter_var( $email, FILTER_VALIDATE_EMAIL );
        list( $user, $domain ) = split( '@', $email );
        $status = checkdnsrr( $domain, 'MX' );
        return $status;
    }
    
    /**
     *   @access public
     *   @param $float     float
     *   @return boolean
    **/
    public function validateFloat( $float )
    {
        return filter_var( $float, FILTER_VALIDATE_FLOAT );
    }
    
    /**
     *   @access public
     *   @param $value     integer
     *   @param $min      integer
     *   @param $max     integer
     *   @return boolean
    **/
    public function validateInt( $value, $min = FALSE, $max = FALSE )
    {
        if( is_numeric( $min ) && is_numeric( $max ) )
        {
            $options = array( 'options' => array( 'min_range' => $min, 'max_range' => $max ) );
            return filter_var( $value, FILTER_VALIDATE_INT, $options );
        }
        else
        {
            return filter_var( $value, FILTER_VALIDATE_INT );
        }
    }
    
    /**
     *   @access  public
     *   @param   $ip     string
     *   @param   $flag     string  
     *   @return  boolean
    **/
    public function validateIp( $ip, $flag = 'FILTER_FLAG_IPV4' )
    {
        return filter_var( $ip, FILTER_VALIDATE_IP, $flag );
    }
    
    /**
     *   @access public
     *   @param $string   string
     *   @param $min     integer
     *   @param $max     integer
     *   @return boolean
    **/
    public function validateLength( $string, integer $min, integer $max )
    {
        if( strlen( $string ) >= $min && strlen( $string ) <= $max )
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    
    /**
     *   @access  public
     *   @param   $url     $url
     *   @return  boolean
    **/
    public function validateUrl( $url )
    {
        return filter_var( $url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED );
    }
    
    /**
     *   @access  public
     *   @param   $email     $email   
     *   @return  string
    **/
    public function sanitizeEmail( $email )
    {
        return filter_var( $email, FILTER_SANITIZE_EMAIL );
    }
    
    /**
     *   @access  public
     *   @param   url     $url
     *   @return  string
    **/
    public function sanitizeUrl( $url )
    {
        return filter_var( $url, FILTER_SANITIZE_ENCODED );
    }
    
    /**
     *   @access  public
     *   @param   $string     $string
     *   @return  string
    **/
    public function sanitizeString( $string )
    {
        return filter_var( $string, FILTER_SANITIZE_STRING );
    }
}

?>
Return current item: Janitor