10个实用的PHP正则表达式汇总,php正则表达式汇总
10个实用的PHP正则表达式汇总,php正则表达式汇总
本文实例讲述了10个实用的PHP正则表达式汇总,分享给大家供大家参考。具体如下:
正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符、词或算式等。但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时。本文为你介绍10种常见的实用PHP正则表达式的写法,希望对你的工作有所帮助。
1. 验证E-mail地址
这是一个用于验证电子邮件的正则表达式。但它并不是高效、完美的解决方案。在此不推荐使用。
复制代码 代码如下:
$email = "test@ansoncheung.tk";
if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format";
}
为了更加有效验证电子邮件地址,推荐使用filer_var 。
复制代码 代码如下:
if (filter_var('test+email@ansoncheung', FILTER_VALIDATE_EMAIL)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format.";
}
2. 验证用户名
这是一个用于验证用户名的实例,其中包括字母、数字(A-Z,a-z,0-9)、下划线以及最低5个字符,最大20个字符。同时,也可以根据需要,对最小值和最大值做合理的修改。
复制代码 代码如下:
$username = "user_name12";
if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) {
echo "Your username is ok.";
} else {
echo "Wrong username format.";
}
3. 验证电话号码
这是一个验证美国电话号码的实例。
复制代码 代码如下:
$phone = "(021)423-2323";
if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x', $phone)) {
echo "Your phone number is ok.";
} else {
echo "Wrong phone number.";
}
4. 验证IP地址
这是一个用来验证IPv4地址的实例。
复制代码 代码如下:
$IP = "198.168.1.78";
if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) {
echo "Your IP address is ok.";
} else {
echo "Wrong IP address.";
}
5. 验证邮政编码
这是一个用来验证邮政编码的实例。
复制代码 代码如下:
$zipcode = "12345-5434";
if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {
echo "Your Zip code is ok.";
} else {
echo "Wrong Zip code.";
}
6. 验证SSN(社会保险号)
这是一个验证美国SSN的实例。
复制代码 代码如下:
$ssn = "333-23-2329";
if (preg_match('/^[\d]{3}-[\d]{2}-[\d]{4}$/',$ssn)) {
echo "Your SSN is ok.";
} else {
echo "Wrong SSN.";
}
7. 验证信用卡号
复制代码 代码如下:
$cc = "378282246310005";
if (preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc)) {
echo "Your credit card number is ok.";
} else {
echo "Wrong credit card number.";
}
8. 验证域名
复制代码 代码如下:
$url = "http://ansoncheung.tk/";
if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}
9. 从特定URL中提取域名
复制代码 代码如下:
$url = "http://ansoncheung.tk/articles";
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];
echo $host;
10. 将文中关键词高亮显示
复制代码 代码如下:
$text = "Sample sentence from AnsonCheung.tk, regular expression has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";
$text = preg_replace("/\b(regex)\b/i", '\1', $text);
echo $text;
希望本文所述对大家的PHP程序设计有所帮助。
[^\d](?
int preg_match ( string $pattern , string $subject [, array $matches [, int $flags ]] )
string $pattern 是一个字符串,所以可以先计算出这个串存入一个变量里
$star='a';
$stop='c';
$info='a1b2c3';
$pattern='/'.$star.'(.+?)'.$stop.'/';
preg_match($pattern,$info,$result);
print_r($result[1]);

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
