PHP code example of mvanvu / php-filter

1. Go to this page and download the library: Download mvanvu/php-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/ */

    

mvanvu / php-filter example snippets


use MaiVu\Php\Filter;

class CustomFilter extends Filter
{
	public static function arrayInteger($value)
	{
		return static::clean($value, 'int:array');
	}
}

// Return '[1, 2, 3]'
echo '<pre>' . print_r(CustomFilter::clean(['1abc2', '2b', 3], 'arrayInteger'), true) . '</pre>';
 php
use MaiVu\Php\Filter;

// Syntax
Filter::clean($source, $type);

// Example
$source = '<h1>Hello World!</h1>';

// Return 'Hello World!'
$filtered = Filter::clean($source, 'string');

// Source array
$source = [
	'<h1>Hello World!</h1>',
	'<h1>Hello VietNam!</h1>',
];

// Return ['Hello World!', 'Hello VietNam!']
$filtered = Filter::clean($source, 'string:array');

// Multi-type
$source = '  <h1>Hello World!</h1>  ';

// Return 'Hello World!'
$filtered = Filter::clean($source, ['string', 'trim']);

 php
use MaiVu\Php\Filter;
Filter::setRule('custom', function($value) {
    return $value . ' is filtered by a Closure';
});

$source = 'Hello World!';

// Return 'Hello World! is filtered by a Closure'
$filtered = Filter::clean($source, 'custom');

// The same above
Filter::clean($source, function($value) {
    return $value . ' is filtered by a Closure';
});

 php

if (isset(static::$rules[$type]))
    {
        $result = call_user_func_array(static::$rules[$type], [$value]);
    }
    elseif (is_callable($type))
    {
	    $result = call_user_func_array($type, [$value]);
    }
    elseif (function_exists($type))
    {
        $result = $type($value);
    }
    else
    {
        $result = $value;
    }
}