Filter for PHP Extension

Source: Internet
Author: User
Introduction

 

Filter. Its function is the same as its name-filter. The filter has multiple built-in common filters. Different filter functions can be divided into two types: sanitization and validation. The difference between the two methods is that the filter clears the non-conforming items in the filtered variables and returns the cleared content. The filter is only a verification function, the value of the variable is not changed. If the value complies with the filter rules, the variable content is returned; otherwise, false is returned;

Install

The installation of filter is very simple. If it is a version later than php5.2.0, it should have been enabled by default. You can view it through phpinfo. For versions earlier than 5.2.0, you can use PECL to use filter.

 

Use

It is easy to use filter to filter or verify data. The following is a simple example.

 

<?php$email1 = 'huanggy@example.org';$email2 = "example.org";$email3 = "(huanggy@example.org)";var_dump(filter_var($email1,FILTER_VALIDATE_EMAIL));var_dump(filter_var($email2,FILTER_VALIDATE_EMAIL));var_dump(filter_var($email3,FILTER_SANITIZE_EMAIL));?>

Run the preceding example. You can see that the filter_var ($ var, $ filter) function uses the filter $ filter to filter the variable $ var, and analyzes the result, the filter_validate_email filter only verifies whether the variable $ email is responsible for the filter rules. If the filter meets the requirements, the variable content is returned. If the variable does not comply with the requirements, false is returned. filter_sanize_email cleans the variable content, clear all content of the rule that is not in charge, and then return the cleared result.

Use of the options Parameter

 

<? PHP $ Int = 10; var_dump (filter_var ($ int, filter_validate_int); # output int (10) $ Options = array ('options' => array ('min _ range' => 15); var_dump (filter_var ($ int, filter_validate_int, $ options )); # output bool (false)?>

In the same integer variable 10, two configuration parameters are added to the second filter. The minimum value is 15, and 10 does not meet this condition. Therefore, fasle is returned.

 

Let's look at the example below.

<? PHP $ Int = '0xf'; var_dump (filter_var ($ int, filter_validate_int); # output bool (false) $ Options = array ('options' => array ('min _ range' => 15), 'flags' => filter_flag_allow_hex); var_dump (filter_var ($ int, filter_validate_int, $ options); # output int (15)?>

The default filter_validate_int filter considers '0xf' as not an integer variable, but adds the flags attachment parameter filter_flag_allow_hex. The filter_flag_allow_hex parameter allows hexadecimal, '0xf' is converted to hexadecimal equal to 15. If the rule is met, 15 is returned.

 

Note: The $ options parameter isArray, Contains two elements: OptionOptions (array)And flags. If only the flags element exists, it can also be passed directly without an array.

Options is an array. The array keyword must be: Option name. For example, the option names in filter_validate_int include min_range and max_range. The option names in filter_validate_float include decimal, and the option names in filter_validate_regexp include Regexp. The array value is defined by the user according to the option name. For example, min_range indicates minimum value filtering. For example, $ Options = array ('options' => array ('min _ range' => 3); then numbers smaller than 3 cannot pass verification.

Instance:

<?php$options = array();$options['options']['min_range'] = 1;$options['options']['max_range'] = 10;$options['flags'] = FILTER_FLAG_ALLOW_OCTAL;filter_var(3, FILTER_VALIDATE_INT, $options);?>

Instance

<?php$foo = '256';$bar = '0x100';var_dump(validate_int($foo)); // false, too largevar_dump(validate_int($bar)); // false, too largefunction validate_int($input){  return filter_var(    $input,    FILTER_VALIDATE_INT,    // We must pass an associative array    // to include the range check options.    array(      'flags'   => FILTER_FLAG_ALLOW_HEX,      'options' => array('min_range' => 1, 'max_range' => 0xff)    )  );}?>

Instance

<?phpecho "|asdf".chr(9).chr(128)."_123|";echo "\n";// "bitwise conjunction" means logic OR / bitwise |echo filter_var("|asdf".chr(9).chr(128)."_123\n|" ,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);/*Results:|asdf    �_123||asdf_123|*/?>

 

I have to say filter_validate_regexp Filter

If the system's built-in verification filter cannot meet the actual needs, we can use the filter_validate_regexp filter to verify the data.

<? PHP // For example, to query keywords, exclude ', ", <,>, and = characters, and limit the length of keywords if (! Filter_input (input_get, 'keyword', filter_validate_regexp, array ("options" => array ("Regexp" => "/^ ('| \" | <| >|= }) /") Exit ('noncompliant keyword '); else echo 'vaild';?>

 

PHP filter_callback Filter

Definition and usage: The filter_callback filter uses user-defined functions to filter values. This filter provides us with full control over data filtering.

The specified function must be stored in the associated array named "options.

Name: "Callback"
ID-number: 1024
Tips and comments
Tip: you can create your own function or use an existing PHP function.

See the following example.

Example 1: Call a UDF

<?phpfunction convertSpace($string){ return str_replace(" ", "_", $string);}$string = "Peter is a great guy!";echo filter_var($string, FILTER_CALLBACK,array("options"=>"convertSpace"));?>

Output: peter_is_a_great_guy!

Example 2: Call A System Function

<?php$string="Peter is a great guy!";echo filter_var($string, FILTER_CALLBACK,array("options"=>"strtoupper"));?>

Output: Peter is a great guy!

Example 3: Call A Class Method

<? Phpclass myfilter {public function filter1 ($ Str) {return str_replace ('9', '6', $ Str) ;}} echo filter_var ('wo9w9w9w9', filter_callback, array ('options' => array ('myfilter', 'filter1'); // call class methods?>

 

In this way, you can customize a filter rule and use the filter_callback filter.

 

Filter Information Query

Visit the following link for more information:

1. http://www.54chen.com/p/intro.filter.html

2. http://www.w3school.com.cn/php/php_ref_filter.asp

3. http://cn2.php.net/manual/en/book.filter.php

 

Reference:

Http://blog.csdn.net/huanggy001/article/details/6149478

Http://www.jb51.net/article/24532.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.