Create Validate.class.php

Usage example:


<?php
include( Validate.class.php );

// set the rules to sanitize and validate.
$rules = array( 
    'name'   => array(
        'required'     => true,
        'alpha_space'  => true,
        'min_length'   => 3,
        'max_length'   => 19 ),

    'id' => array(
        'required'     => true,
        'numeric'      => true,
        'validate_int' => true ) );

// form was submitted.
if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
    // Initiate a new validator using the post rules.
    $validator = new Validate( $_POST, $rules );

    // if validation passed return clean data or error.
    if( $validator->validate() )
    {
        $vars = $validator->getFields();
        // code that uses validated fields
    } else {
        $error = $validator->getError();
        // code that uses error messege
    }
}