Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Filters Advanced in PHP

Filters Advanced in PHP

? Advanced Filters in PHP

PHP provides a rich set of advanced filters to validate and sanitize various types of input more precisely. In addition to basic sanitization and validation, you can also apply more advanced filtering options to ensure that input data adheres to complex rules, ranges, and patterns.


? 1. Validation with Ranges and Options

You can use the options parameter in certain filters to set custom validation rules like specifying a range for integers, or checking if a string matches a specific pattern.

Example: Validate Integer within a Range

$age = 25;$options = [    "options" => [        "min_range" => 18,        "max_range" => 99    ]];if (filter_var($age, FILTER_VALIDATE_INT, $options)) {    echo "Valid age!";} else {    echo "Age is out of range.";}

? Here, FILTER_VALIDATE_INT checks that the age is an integer and between 18 and 99.


? 2. Filter with Regular Expressions (FILTER_VALIDATE_REGEXP)

The FILTER_VALIDATE_REGEXP allows you to validate input data against a custom regular expression.

Example: Validate Custom Pattern (e.g., Phone Number)

$phone = "+1-123-456-7890";$pattern = "/^\+?\d{1,3}-\d{3}-\d{3}-\d{4}$/";if (filter_var($phone, FILTER_VALIDATE_REGEXP, ["options" => ["regexp" => $pattern]])) {    echo "Valid phone number.";} else {    echo "Invalid phone number.";}

? The regexp option allows you to specify any regex pattern to match against input data.


? 3. Filter with Callback Function (FILTER_CALLBACK)

PHP allows you to pass a custom callback function to filter input data. This can be useful if you need to perform more complex validation or sanitization logic.

Example: Using a Callback to Sanitize Input

function sanitize_name($value) {    return ucwords(strtolower(trim($value))); // Capitalize and trim spaces}$name = "  jOhN dOE  ";$sanitized_name = filter_var($name, FILTER_CALLBACK, ["options" => "sanitize_name"]);echo $sanitized_name; // Output: John Doe

? Here, FILTER_CALLBACK allows you to define a custom sanitization function for input data.


? 4. Multiple Filters with filter_var_array()

If you need to validate and sanitize multiple fields at once, filter_var_array() lets you apply multiple filters to different keys in an associative array.

Example: Validate and Sanitize Multiple Inputs

$data = [    "email" => "example@domain.com",    "age" => "25",    "url" => "https://example.com"];$filters = [    "email" => FILTER_VALIDATE_EMAIL,    "age" => [        "filter" => FILTER_VALIDATE_INT,        "options" => ["min_range" => 18, "max_range" => 100]    ],    "url" => FILTER_VALIDATE_URL];$filtered_data = filter_var_array($data, $filters);if ($filtered_data) {    echo "Valid inputs: ";    print_r($filtered_data);} else {    echo "Invalid input.";}

? filter_var_array() allows you to validate and sanitize multiple inputs in one call, returning an array of filtered results.


? 5. Custom Validation: FILTER_CALLBACK Example

You can create more advanced logic with a callback function, such as checking if a string matches an accepted list of values, or performing more complex operations.

Example: Custom Callback Validation

function validate_username($username) {    // Username must start with a letter and be between 5-15 characters long    return preg_match("/^[a-zA-Z][a-zA-Z0-9]{4,14}$/", $username);}$username = "john_doe";if (filter_var($username, FILTER_CALLBACK, ["options" => "validate_username"])) {    echo "Valid username!";} else {    echo "Invalid username!";}

? FILTER_CALLBACK can execute any function, allowing you to apply complex validation or sanitization rules to inputs.


? 6. Using FILTER_DEFAULT for Validation

For standard types like email, IP, etc., PHP offers default filters that handle common validation automatically.

$email = "user@example.com";if (filter_var($email, FILTER_VALIDATE_EMAIL)) {    echo "Valid email address!";} else {    echo "Invalid email address!";}

? Common filters:

  • FILTER_VALIDATE_EMAIL

  • FILTER_VALIDATE_URL

  • FILTER_VALIDATE_IP


? 7. Filter Input Using filter_input() and filter_input_array()

For superglobal inputs like GET, POST, COOKIE, you can use filter_input() for single fields or filter_input_array() for multiple fields.

Example: Validate a POST field

// POST data: 'age' = 25$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT, [    "options" => ["min_range" => 18, "max_range" => 99]]);if ($age) {    echo "Valid age!";} else {    echo "Invalid age!";}

? Best Practices with Advanced Filters

  • Use custom patterns (regex) for precise data validation (e.g., credit card, phone numbers).

  • Apply multiple filters in filter_var_array() for batch validation and sanitization.

  • Validate data before storing in a database or performing sensitive operations.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql