PHP code example of sugiphp / filter

1. Go to this page and download the library: Download sugiphp/filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sugiphp / filter example snippets



	Filter::int($value, $min = false, $max = false, $default = false);
	// Examples:
	Filter::int(0); // 0
	Filter::int(""); // false
	Filter::int(1.0); // 1
	Filter::int(1.1); // false
	Filter::int(1, 2); // false - outside the minimum range
	Filter::int(5, 2, 4); // false - outside maximum allowed value
	Filter::int("1"); // 1
	Filter::int("1.0"); // false
	Filter::int("1a"); // false
	Filter::int("hi", false, false, 77); // 77 - Returns the default value


	// Validate integer from GET parameter - $_GET["key"].
	// Default value is returned if the key is not found, or cannot be converted to an integer,
	// or the value is outside the min / max range.
	Filter::intGet($key, $min_range = false, $max_range = false, $default = false);
	// for example if the URL is http://example.com?page=12
	Filter::intGet("page", 1, false, 1); // returns 12
	Filter::intGet("foo"); // returns FALSE

	// Validate integer from POST parameter - $_POST["key"].
	// Works like intGet()
	Filter::intPost($key, $min_range = false, $max_range = false, $default = false);

	// Validate integer from COOKIES - $_COOKIE[$key]
	Filter::intCookie($key, $min_range = false, $max_range = false, $default = false);

	// and from SESSION
	Filter::intSession($key, $min_range = false, $max_range = false, $default = false);


	Filter::str($value, $minLength = 0, $maxLength = false, $default = false);
	// Examples:
	Filter::str("a"); // "a"
	Filter::str(1); // "1"
	Filter::str(" a "); // "a"
	Filter::str(""); // ""
	Filter::str("", 1); // false
	Filter::str(" a ", 1); // "a"
	Filter::str("ab", 1, 1); // false
	Filter::str("abc", 1, 2, "error"); // "error"
	Filter::str("abc", 1, false, "error"); // "abc"

	// Slightly different version of the Filter::str() method is Filter::plain()
	// This will firstly strip all tags and then it will act exactly like Filter::str() method.
	Filter::plain($value, $minLength = 0, $maxLength = false, $default = false);


	Filter::strGet($key, $minLength = 0, $maxLength = false, $default = false);
	Filter::strPost($key, $minLength = 0, $maxLength = false, $default = false);
	Filter::strCookie($key, $minLength = 0, $maxLength = false, $default = false);
	Filter::strSession($key, $minLength = 0, $maxLength = false, $default = false);

	// Validates plain text from $_GET, $_POST, $_COOKIE and $_SESSION parameters
	Filter::plainGet($key, $minLength = 0, $maxLength = false, $default = false);
	Filter::plainPost($key, $minLength = 0, $maxLength = false, $default = false);
	Filter::plainCookie($key, $minLength = 0, $maxLength = false, $default = false);
	Filter::plainSession($key, $minLength = 0, $maxLength = false, $default = false);


	Filter::url($value, $default = false);
	// Examples:
	Filter::url("http://igrivi.com"); // true
	Filter::url("igrivi.com"); // false
	Filter::url("http://localhost"); // false - The filter is mainly used for user inputs, so when we need URL, we intentionally don't want localhost
	Filter::url("8.8.8.8"); // false
	Filter::url("http://somedomain.com:81"); // true
	Filter::url("http://somedomain.com:6"); // false


	Filter::email($value, $default = false, $checkMxRecord = false);


	// Checks the existence of the key in a given array, returning default value if $key is not present.
	Filter::key($key, $array, $default = null);
	// Example:
	Filter::key("foo", array("one", "foo" => "bar", "foobar" => 2)); // "bar"

	// Validates $_GET[$key] value.
	Filter::get($key, $default = null);
	// Validates $_POST[$key] value.
	Filter::post($key, $default = null);
	// Validates $_COOKIE[$key] value.
	Filter::cookie($key, $default = null);
	// Validates $_SESSION[$key] value.
	Filter::session($key, $deafult = null);