In php programs, regular expressions are generally used to verify email addresses, IP addresses, and URLs. In fact, some Built-in validation rules are provided in php. The following example describes how to use the built-in filter function of php to verify email, ip, and URL.
1. verify email
$ Email = 'phpernote @ 163.com '; $ result = filter_var ($ email, FILTER_VALIDATE_EMAIL); var_dump ($ result); // output: string (17) 'phpernote @ 163.com'
2. Verify the url address
$ Url = 'HTTP: // www.phpernote.com/'; $ result = filter_var ($ url, FILTER_VALIDATE_URL); var_dump ($ result); // output: string (25) 'HTTP: // www.phpernote.com /'
3. Verify the IP address
$ Url = '192. 168.0.1 '; $ result = filter_var ($ url, FILTER_VALIDATE_IP); var_dump ($ result); // output: string (11) '192. 168.0.1'
The filter function in php can also be used to verify floating-point numbers, integer numbers, and Boolean types. For details, refer to the filter function in the php manual or refer to the following article on this site:
PHP filter_var () function Filter function
Articles you may be interested in
- Common JS function collection (remove spaces, verify email addresses, dates, positive integers, etc)
- PHP filter_var () function Filter function
- Php uses array_flip to implement array key-value exchange to remove array duplicate values
- Collect common php functions with explanations
- PHP string escape functions (addslashes, stripslashes)
- Php performance optimization: Using isset () to determine the string length is faster than strlen ()
- Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
- URL encoding functions such as urlencode (), urldecode (), rawurlencode (), and rawurldecode ()