PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Kenth Hagström   Janitor   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage of methods
Class: Janitor
Validate and sanitize different types of values
Author: By
Last change:
Date: 14 years ago
Size: 2,505 bytes
 

Contents

Class file image Download
<?php

/**
 * Example usage of the janitor class
 *
 * If you run this example, you should see this:
 
 TRUE
 TRUE
 FALSE
 TRUE
 FALSE
 TRUE
 FALSE
 TRUE
 test@test.com
 http%3A%2F%2Fwww.google.com%2F
 removing tags from me
 
**/
echo "<pre>";
include(
'janitor.class.php');
$janitor = new janitor();

// Validate an alphabetical string
$string = 'hello';
if(
$janitor->validateAlpha( $string ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}

// Validate an alphanumerical string
$string = 'hello123';
if(
$janitor->validateAlphanum( $string ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}

// Validate an email adress, also check for MX on domain
$email = 'test@test.com';
if(
$janitor->validateEmail( $email ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}

// Validate an email adress, also check for MX on domain
$float = '10.2';
if(
$janitor->validateFloat( $float ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}


// Validate an integer, optionally you can set a min and max value like in this example
// if you exclude the min and max parameters no range limit will apply
$integer = 4;
if(
$janitor->validateInt( $integer, 5, 10 ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}

// Validate an IP adress, optionally you can supply a second parameter with a filter flag
// Available flags are: FILTER_FLAG_IPV4, FILTER_FLAG_IPV6, FILTER_FLAG_NO_PRIV_RANGE, FILTER_FLAG_NO_RES_RANGE
$ip = '192.168.0.34';
if(
$janitor->validateIp( $ip ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}

// Validate length of a string, you also have to set the min and max number of characters allowed, useful for password and username lengths etc.
// store static values in constants like below etc, or just set the values when you're calling the method
define( 'max_length', 10 );
define( 'min_length', 3 );
$password = 'checking my length';
if(
$janitor->validateIp( $password ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}

// Validate an URL
$url = 'http://www.google.com/';
if(
$janitor->validateUrl( $url ) )
{
    echo
"TRUE\n";
}
else
{
    echo
"FALSE\n";
}

// Sanitize email
$email = 'test@test.com';
$cleanEmail = $janitor->sanitizeEmail( $email );
echo
$cleanEmail."\n";

// Sanitize URL
$url = 'http://www.google.com/';
$cleanUrl = $janitor->sanitizeUrl( $url );
echo
$cleanUrl."\n";

// Sanitize string,
$string = "removing tags <b>from me</b>";
$cleanString = $janitor->sanitizeString( $string );
echo
$cleanString."\n";
echo
"</pre>";

?>