Before reading this function, I verified that the mailbox, IP address, and url are all processed using regular expressions. today, I found that the filter function can replace regular expressions and the method is simple and easy to use, the following example describes how to use this function. I didn't know filt when I used php in my early years... before reading this function, I verified that the mailbox, IP address, and url are all processed using regular expressions. today, I found that the filter function can replace regular expressions and the method is simple and easy to use, the following example describes how to use this function.
When I was using php in the early years, I still didn't know the filter feature. at that time, I used regular expressions to determine whether the mailbox, url, and IP address format were correct. later, as I went deeper, in php, you can also use the built-in function library filter to complete these functions.
1. verify emailFirst, let's look at the original regular expression verification. the code is as follows:
Let's look at the filter code as follows:
$email = 'sjlinyu@qq.com'; $result = filter_var($email, FILTER_VALIDATE_EMAIL); var_dump($result); //string(14) "sjlinyu@qq.com"
For the filter_var function, if the verification succeeds, the verification object is returned; otherwise, false is returned, which makes the latter simpler.
2. verify the url address and regular expression. the code is as follows:
The code of the filter_var function is as follows:
$url = "http://www.phprm.com"; $result = filter_var($url, FILTER_VALIDATE_URL); var_dump($result); //string(22) "http://www.phprm.com"
3. verify the IP address and the regular expression function. the code is as follows:
/*** Check whether the IP address is correct. */Function checkipaddres ($ ipaddres) {$ preg = "/A ([0-9]? [0-9]) | (1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]).) {3} ([0-9]? [0-9]) | (1 [0-9] {2}) | (2 [0-4] [0-9]) | (25 [0-5]) Z/"; if (preg_match ($ preg, $ ipaddres) return true; return false ;}
The code for this function verification is as follows:
$url = "192.168.1.110"; $result = filter_var($url, FILTER_VALIDATE_IP); var_dump($result); //string(13) "192.168.1.110"
This method can also be used to verify ipv6. the code is as follows:
$url = "2001:DB8:2de::e13"; $result = filter_var($url, FILTER_VALIDATE_IP); var_dump($result); //string(17) "2001:DB8:2de::e13"
4. check whether the value is an integer within an integer range. the code is as follows:
$ I = '010 '; $ result = filter_var ($ I, FILTER_VALIDATE_INT, // set the value range of the check value array ('options' => array ('min _ range' => 1, 'max _ range' => 100 ))); var_dump ($ result); // bool (false)
Php variables are of a weak type. If a filter is not used, it is true to use a variable greater than or less than the symbol. the code is as follows:
$i = '010'; $result = $i >= 1 && $i <= 100; var_dump($result);//bool(true)
5. verify the floating point. The code is as follows:
$float = 12.312; $result = filter_var($float, FILTER_VALIDATE_FLOAT); var_dump($result); //float(12.312)
When verifying the amount, you often need to verify whether the amount is a floating point number.
Summary: Although the filter in php is relatively unpopular, it is quite powerful. in addition to the above functions, there are also some filtering input functions. you can refer to the php Manual.
Link to this article:
Add to favorites ^ please keep the tutorial address.