filter in php

WBOY
Release: 2016-07-28 08:27:46
Original
1078 people have browsed it

Verification string is the most common function in the program. For example, when encountering such a scenario, verify whether the string conforms to the IP address format. There are usually two methods:

​ (1) Handwriting an IP address verification function

​ (2) Regular expression

​ When using C language to deal with such problems, it usually takes more than ten minutes to write a verification function by yourself function. When dealing with such problems using languages ​​that support regularization (php, C++11, etc.), regularization is usually used. However, regular expressions are difficult to use and not intuitive to read. (This does not mean that regularity is not important). Sometimes we just want to quickly solve the problem at hand, so what should we do? At least in php we can use filters.

         Filters are specifically used through filtering functions, such as filter_var to determine whether a variable meets the requirements

                                            use using  to  use             through using   out out out through out out out through off ‐ through off ‐  ‐ ‐‐ ‐ to to verify whether the IP and email are valid:

   <?php

    $ip="192.168.0.222";
    $is_ip=filter_var($ip,FILTER_VALIDATE_IP);
    var_dump($is_ip);
    $mail="123@xx.com";
    $is_mail=filter_var($mail,FILTER_VALIDATE_EMAIL);
    var_dump($is_mail);
   ?>
Copy after login

‐ This is better than checking the string by yourself or Regular expressions are much more convenient~

There are three types of filters in php: filters with the word VALIDATE only check whether the string is valid and will not change the status of the string. Filters with SANITIZE will delete substrings that do not meet the requirements, and the third type is FILTER_CALLBACK. That is, use your own callback function for verification.

A table is provided on the w3cschool online website detailing the functions of each filter and filter function in PHP.

PS: The filter is provided by default in versions after PHP5.2. Previous versions require an extension to be installed to use it. Although filters are easy to use, their functions are still relatively limited. When facing specific needs, they need to be used in conjunction with string libraries and regular expressions.

The above introduces the filters in PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!