filter_var() 验证邮箱、ip、url的格式 php
验证邮箱格式的正确与否;你的第一解决方法是什么呢?
不管你们怎 么[思] [考] 的;反正我首先想到的就是字符串查找看是否有@符号 ;
但是对于结尾的.com或者.net 亦或者.cn等等越来越多的域名验证感觉棘手;
直到了某一天认真的研究了filter_var()函数的参数;
我对以前走马观花式的阅读php文档表示深深的惭愧;
其实filter_var()第二个参数传FILTER_VALIDATE_EMAIL即可验证邮箱、略加整理就如下;
/** * 验证是否是邮箱 * @param string $email 邮箱 * @return boolean 是否是邮箱 */function is_email($email){ if(filter_var($email,FILTER_VALIDATE_EMAIL)){ return true; }else{ return false; }}var_dump(is_email('baijunyao@baijunyao.com'));// 打印 turevar_dump(is_email('baijunyao.com'));// 打印 false
经研究;这个函数验证邮箱的规则就是要有个@符号 ;然后@符号后面还要有个英文句号. ;
咦?看到baijunyao.com;那有木有一个可以验证url域名的函数?
恩;没错;有的;第二个参数传FILTER_VALIDATE_URL就是用来验证url的;
/** * 验证是否是url * @param string $url url * @return boolean 是否是url */function is_url($url){ if(filter_var($url,FILTER_VALIDATE_URL)){ return true; }else{ return false; }}var_dump(is_url('http://baijunyao.com'));// 打印 truevar_dump(is_url('baijunyao.com'));// 打印 falsevar_dump(is_url('http://a'));// 打印 true
根据打印的结果可知;判断是否是url的规则就是要有http:// 后边要跟一个字母数字即可;
当然;顺理成章;第二个参数传FILTER_VALIDATE_IP的时候就是验证是否是ip地址了;
filter_var很强大的样子;那传个FILTER_VALIDATE_PHONE 是不是就可以验证手机号了?
恩恩;;;da an是否定的;你想多了;
别看了;这是做文章封面图用的;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
