本文章来详细的介绍一在我们php开发中常用的几种正则表达式,如有网址、email、手机号码正则表达代码
1. 判断Email:
域名由各国文字的特定字符集、英文字母、数字及“-”(即连字符或减号)任意组合而成, 但开头及结尾均不能含有“-”,“-”不能连续出现 。 域名中字母不分大小写。域名最长可达60个字节(包括后缀.com、.net、.org等)。
/^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?$/i;
/内容/i 构成一个不区分大小写的正则表达式;
基本规则如下
代码如下 | 复制代码 |
preg_match('/^[a-z0-9_-]+(.[_a-z0-9-]+)*@([_a-z0-9-]+.)+([a-z]{2} |aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/',$email) |
例1
代码如下 | 复制代码 |
function is_email($email){ |
例2
代码如下 | 复制代码 |
$email_pattern = '/w{6,16}@w{1,}.w{2,3}/i'; $email_valid = 'test_123@126.net'; $email_invalid = 'test@test%@111@com'; $matches = array(); preg_match($email_pattern, $email_valid, $matches[]); preg_match($email_pattern, $email_invalid, $matches[]); var_dump($matches); ?> 结果 array(2) { [0]=> array(1) { [0]=> string(16) "test_123@126.net" } [1]=> array(0) { } } |
2. 判断Url:
例1
代码如下 | 复制代码 |
function is_url($str){ |
例2
php判断url地址并自动转换为超链接,在一段字符串中用正则表达式匹配出url,在将url转换为超链接,点击可访问地址
代码如下 | 复制代码 |
function autolink($foo) ?> |
3. 判断手机号码:
例1
代码如下 | 复制代码 |
function is_mobile($str){ 例2 if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$mobilephone)){ |

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

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