search
HomeBackend DevelopmentPHP TutorialWhat are PHP regular expressions? How to use PHP regular expressions (with code)

What is PHP regular expression? PHP regular expression is a grammatical rule that describes the structure of a string. It is a specific formatting pattern that can match, replace, and intercept matching strings. So, how to use PHP regular expression? Next let's take a look at specific examples.

What are PHP regular expressions? How to use PHP regular expressions (with code)

1. Introduction to regular expressions:

Regular expressions It is a grammatical rule used to describe character arrangement and matching patterns. It is mainly used for pattern segmentation, matching, search and replacement operations of strings.

1. Purpose: matching, search, replace, split

2. PHP provides two sets of regular expression function libraries

*1. Perl Compatible with regular expression functions (recommended) ​
​ 2. POSIX extended regular expression function

2. Syntax: ​

Expression The format of the expression: "/expression/[modifier]"

Explanation: "/" represents the delimiter of the regular expression, but it can also be other symbols: such as "#", "!"

Note: The delimiter cannot be letters, numbers and slashes\.

Like "#", "|", "!", etc.
Such as: /.../ #...# |....|

Among them Modifiers are optional and represent additional modifications to the expression.

3. Components of regular expressions:

1. Atoms are the basic units that make up regular expressions. When analyzing The regular expression should be used as a whole.

Including the following:

# & gt; single character, number, such as A-Z, A-Z, 0-9.​
            > Pattern units, such as (ABC) can be understood as large atoms composed of multiple atoms.
& gt; atomic table, such as [ABC].
                                                                                                                                                                                             . . Atomic table of characters

For example: [aoeiu] represents any vowel letter

[0-9] represents any digit

[a-z][0-9 ] represents two characters composed of lowercase letters and one digit. # Indicates that any character other than the atoms in square brackets is the negation of []. Any non-lowercase letter

{m} represents the control of the number of previous atoms, indicating m times

For example: [0-9]{4} means 4 is a number

                                                                                                                                                                   ,                           For example: [0-9]{2,} represents two or more digits

                                                                                                                                                                                                                    . Indicates the control of the number of the previous atoms, indicating any number of times, equivalent to {0,}  
                                                                                               .                    The number of atoms is controlled, indicating 0 or 1 time (optional). Equivalent to {0,1}

For example: positive integer: [1-9][0-9]* \-]?[0-9]  

                                                                                            using using using using ’             through ’ s ’ ’ s ’     through through using ’ s ’     through ’ through through through through ‐ ‐ ‐ ‐‐‐‐‐]? 
                                                                                                                                                                                                                                                                        Can also use ?: To reject substores. (?:*?)
For example: (red) string red
(rea | blue) string red or blue
(abc) {2} indicates two abc
| Meaning
(Rea | Blue) String Red or Blue
^is used at the beginning of the regular unit block, indicating that it must be used at the end of the specified
$ at the end of the block block, indicating that it must be based on the end of the block block, indicating that it must be based on the end of the block, which means that it must be based on the must be used. The specified end
. Represents any character other than a newline character
Commonly used combinations: .*? Represents a minimum match of all characters (rejecting greedy matching)


3. Ordinary escape Characters:


##\d matches a number; equivalent to [0-9]

\D matches any character except numbers; equivalent to [^0-9]##\w\W\s Matches a whitespace character; equivalent to [\f\n\r \t\v] \S Matches any character except whitespace characters; equivalent to [^\f\n\r\t \v]##\f matches a form feed and is equivalent to \x0c or \cL Matches a newline character; equivalent to \x0a or \cJ Matches a carriage return character equivalent to \ x0d or \cM matches a tab; equivalent to \x09\ or \cl Matches a vertical tab; equivalent to \x0b or \ck Matches an octal number Matches a hexadecimal numberMatch a control character
matches an English letter, Numbers or underscores; equivalent to [0-9a-zA-Z_]
matches any character except English letters, numbers and underscores; etc. Equivalent to [^0-9a-zA-Z_]


##\n
##\r
\t
\v

\oNN

\xNN
\cC

         4. 模式修整符    
        i 表示不区分大小写;    
            "/[a-zA-Z]/" "/[a-z]/i"    
        s 表示匹配视为单行(就是可以让点.支持换行)    
        U 表示拒绝贪婪匹配

四、 php正则表达式函数:    

    preg_grep --  返回与模式匹配的数组单元     

    * preg_match_all -- 进行全局正则表达式匹配 , 返回共计匹配的个数。    
        和下面的一样,不同的是匹配到最后(全局匹配)     

    * preg_match -- 进行正则表达式匹配,只匹配一次,返回1,否则0,    
        格式:preg_match("正则表达式","被匹配的字串",存放结果的变量名,PREG_OFFSET_CAPTURE,起始偏移量)    
        其中:PREG_OFFSET_CAPTURE表示获取匹配索引位置    
              起始偏移量:从指定位置开始匹配     

    preg_quote -- 转义正则表达式字符     

    preg_split -- 用正则表达式分割字符串     

    preg_replace -- 执行正则表达式的搜索和替换

实例:

1.php正则表达式匹配

//正则匹配函数preg_match()

//模糊匹配(包含形式)
//if(preg_match("/a/","qwertayuio")){ //匹配字串中是否包含a字符
//if(preg_match("/(abc)/","qwerta bcayuio")){ //匹配字串中是否包含abc字串
//if(preg_match("/[abc]/","qwertbycuiop")){ //匹配字串中是否包含a、b或c字字符
//if(preg_match("/[0-9]/","qwertbycuiop")){ //匹配字串中是否包数字
//if(preg_match("/[a-z]/","12345a6789")){ //匹配字串中是否包小写字母
//if(preg_match("/[0-9]{2}/","qwe89rqw9re8qwer",$a)){ //匹配字串中是否包两位的数字
//if(preg_match("/[0-9]{2,}/","qwe12rqw9re8qwer",$a)){ //匹配字串中是否包至少两位的数字
if(preg_match("/[0-9]{2,4}/","qwe12567rqw9re8qwer",$a)){ //匹配字串中是否包至少两位到4位的数字
    echo "匹配!";
}else{
    echo "不匹配!";
}

echo $a[0];   

echo "<hr/>";

//精确匹配
//if(preg_match("/^[0-9]{2}$/","34")){ //精确匹配两位数字
if(preg_match("/^[1][35][0-9]{9}$/","13520319616")){ //匹配一个手机号码
    echo "匹配!";
}else{
    echo "不匹配!";
}

What are PHP regular expressions? How to use PHP regular expressions (with code)

What are PHP regular expressions? How to use PHP regular expressions (with code)

2.php正则的元字符匹配

//正则的元字符使用
//检测是否是一个合法的mail地址
if(preg_match("/^[\w\.]+@[\w]+(\.[a-zA-Z]+){1,3}$/","asd@asd.com")){
    echo "正确";
}else{
    echo "错误";
}

/*
//检测是否是一个十六进制整数(正整数,负整数,0)
if(preg_match("/^-?0[xX][\da-fA-F]+$/","-2")){
    echo "正确";
}else{
    echo "错误";
}

//检测是否是一个整数(正整数,负整数,0)
//if(preg_match("/^-?[0-9]+$/","-2")){
if(preg_match("/^-?\d+$/","-2")){
    echo "正确";
}else{
    echo "错误";
}
*/

/*
//检测一个变量名是否正确
//if(preg_match("/^[a-zA-Z_][a-zA-Z0-9_]*$/","1a5b_c")){
if(preg_match("/^[a-zA-Z_][\w]*$/","a5b_c")){
    echo "正确";
}else{
    echo "错误";
}
*/

//匹配字串中的4位数字
//preg_match("/[0-9]{4}/","qweabi123srqcdwer456iabs7890asfcd",$a);
//preg_match("/\d{4}/","qweabi123srqcdwer456iabs7890asfcd",$a);
//var_dump($a[0]); //匹配:7890

//preg_match_all("/(ab|cd)/","qweabisrqcdweriabsasfcd",$a);
//var_dump($a[0]); //匹配字串所有ab或cd

//preg_match_all("/is/","qweisrqwerisasfd",$a);
//var_dump($a); //匹配字串所有is

//preg_match("/.*/","*a\nbc",$a);
//var_dump($a); //*a

3.php正则表达式匹配网页

//正则匹配函数:preg_match   preg_match_all
$str=<<<yfstr
    <div id="mainNav" class="clearfix">
        <a href="index.php">首页</a>
        <a href="category.php?id=3">GSM手机</a>
       <a href="category.php?id=4">双模手机</a>
       <a href="category.php?id=6">手机配件</a>
       <a href="group_buy.php">团购
       商品</a>
       <a href="activity.php">优惠活动</a>
       <a href="snatch.php">夺宝奇兵</a>
       <a href="auction.php">拍卖活动</a>
       <a href="exchange.php">积分商城</a>
       <a href="message.php">留言板</a>
       <a href="http://bbs.ecshop.com/">EC论坛</a>
 </div>
yfstr;

echo "<table width=&#39;900&#39; border=&#39;1&#39;>";
echo "<tr><th>名称</th><th>URL地址</th><th>链接</th></tr>";
//使用正则匹配
preg_match_all("/<a href=\"(.*?)\".*?>(.*?)<\/a>/s",$str,$a);
foreach($a[0] as $k=>$v){
    echo "<tr>";
    echo "<td>{$a[2][$k]}</td>";
    echo "<td>{$a[1][$k]}</td>";
    echo "<td>{$v}</td>";
    echo "</tr>";
}
echo "</table>";

注:使用

使用格式:

<<<EOF...EOF;

使用定界符无需给双引号增加转义字符,可以参考如下:

$str=”/

class=\"clearfix\">/”;

4正则的其他函数使用

//正则的其他函数使用:
//preg_quote -- 转义正则表达式字符
//preg_split -- 用正则表达式分割字符串
//preg_replace -- 执行正则表达式的搜索和替换

//1.preg_quote -- 转义正则表达式字符
echo preg_quote("(abc){10}","&#39;");//在每个增则表达式语法的字符前增加一个反斜杠

$s = "a{4}";
preg_match("/".preg_quote($s)."/","werta{4}yu",$a);
var_dump($a);

echo "<br/>";
//2. preg_split -- 用正则表达式分割字符串
$s = "12,34:56;784;35,67:897:65";
$list = preg_split("/[,:;]/",$s);
var_dump($list);

echo "<hr/>";

//3. preg_replace执行正则表达式的搜索和替换
$s = "12,34:56;784;35,67:897:65";
//要求将上面的:,;都换成空格
echo preg_replace("/[,;:]/"," ",$s);

$str = "<ul style=&#39;color:red&#39;>
            <li>aaaaa</li>
            <li>bbbbb</li>
            <li>ddddd</li>
            <li>eeeee</li>
        </ul>";

//将上面字串中所有li标签中都添加一个b标签。
echo "<hr/>";
echo $str;

echo "<hr/>";
//echo preg_replace("/<li>(.*?)<\/li>/","<li><b>\\1</b></li>",$str);
//echo preg_replace("/<li>(.*?)<\/li>/","<li><b>\$1</b></li>",$str);
echo preg_replace("/<li>(.*?)<\/li>/",&#39;<li><b>$1</b></li>&#39;,$str);

5.子存储(扩展)

//子存储使用
$date="[2012-08-09],[2012,09-19],[2011/08,09],[2012/10/09],[2013,08,01]";

//将上面字串中合法的日期匹配出来
preg_match_all("/\[[0-9]{4}([\-,\/])[0-9]{2}\\1[0-9]{2}\]/",$date,$a);
var_dump($a);

echo "<hr/>";

$str = "<ul 
style=&#39;color:red&#39;><br/>
            <li>aaaaa</li>
            <li>bbbbb</li>
            <li>ddddd</li>
            <li>eeeee</li>
        </ul>";
//将上面字串中的html标记删除掉(替换空)
echo  preg_replace("/<\/?.*?\/?>/s","",$str);

附录

php常用正则表达式

^\d+$  \d 是代表0-9  $必须要以....结束  这是代表非负整数   正则表达式

平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 
"^\d+$"  //非负整数(正整数 + 0) 
"^[0-9]*[1-9][0-9]*$"  //正整数 
"^((-\d+)|(0+))$"  //非正整数(负整数 + 0) 
"^-[0-9]*[1-9][0-9]*$"  //负整数 
"^-?\d+$"    //整数 
"^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0) 
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数 
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0) 
"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数 
"^(-?\d+)(\.\d+)?$"  //浮点数 
"^[A-Za-z]+$"  //由26个英文字母组成的字符串 
"^[A-Z]+$"  //由26个英文字母的大写组成的字符串 
"^[a-z]+$"  //由26个英文字母的小写组成的字符串 
"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串 
"^\w+$"  //由数字、26个英文字母或者下划线组成的字符串 
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"    //email地址 
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"  //url 
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日 
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年 
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil 
/^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$/ //电话号码 
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP地址 
匹配中文字符的正则表达式: [\u4e00-\u9fa5] 
匹配双字节字符(包括汉字在内):[^\x00-\xff] 
匹配空行的正则表达式:\n[\s| ]*\r 
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 
匹配首尾空格的正则表达式:(^\s*)|(\s*$) 
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
匹配网址URL的正则表达式:^[a-zA-z]+://([url=file://\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$]\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$[/url] 
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 
匹配国内电话号码:(\d{3}-|\d{4}-)?(\d{8}|\d{7})? 
匹配腾讯QQ号:^[1-9]*[1-9][0-9]*$ 

元字符及其在正则表达式上下文中的行为: 
\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。 
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的Multiline 属性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。 
$ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的Multiline 属性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。 
* 匹配前面的子表达式零次或多次。 
+ 匹配前面的子表达式一次或多次。+ 等价于 {1,}。 
? 匹配前面的子表达式零次或一次。? 等价于 {0,1}。 
{n} n 是一个非负整数,匹配确定的n 次。 
{n,} n 是一个非负整数,至少匹配n 次。 
{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。在逗号和两个数之间不能有空格。 
? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认 
的贪婪模式则尽可能多的匹配所搜索的字符串。 
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 ’\n’ 在内的任何字符,请使用象 ’[.\n]’ 的模式。 
(pattern) 匹配pattern 并获取这一匹配。 
(?:pattern) 匹配pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。 
(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。 
(?!pattern) 负向预查,与(?=pattern)作用相反 
x|y 匹配 x 或 y。 
[xyz] 字符集合。 
[^xyz] 负值字符集合。 
[a-z] 字符范围,匹配指定范围内的任意字符。 
[^a-z] 负值字符范围,匹配任何不在指定范围内的任意字符。 
\b 匹配一个单词边界,也就是指单词和空格间的位置。 
\B 匹配非单词边界。 
\cx 匹配由x指明的控制字符。 
\d 匹配一个数字字符。等价于 [0-9]。 
\D 匹配一个非数字字符。等价于 [^0-9]。 
\f 匹配一个换页符。等价于 \x0c 和 \cL。 
\n 匹配一个换行符。等价于 \x0a 和 \cJ。 
\r 匹配一个回车符。等价于 \x0d 和 \cM。 
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。 
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 
\t 匹配一个制表符。等价于 \x09 和 \cI。 
\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。 
\w 匹配包括下划线的任何单词字符。等价于’[A-Za-z0-9_]’。 
\W 匹配任何非单词字符。等价于 ’[^A-Za-z0-9_]’。 
\xn 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。 
\num 匹配 num,其中num是一个正整数。对所获取的匹配的引用。 
\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个 
八进制转义值。 
\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至 
少有 n 个获取,则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足,若 n 和 m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm。 
\nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八进制转义值 nml。 
\un 匹配 n,其中 n 是一个用四个十六进制数字表示的Unicode字符。 
匹配中文字符的正则表达式: [\x{4e00}-\x{9fa5}] 
匹配双字节字符(包括汉字在内):[^x00-xff] 
匹配空行的正则表达式:n[s| ]*r 
匹配HTML标记的正则表达式:/<(.*)>.*|<(.*) />/ 
匹配首尾空格的正则表达式:(^s*)|(s*$) 
匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 
匹配网址URL的正则表达式:[url=http://([w-]+.)+[w-]+(/[w]http://([w-]+.)+[w-]+(/[w[/url]- ./?%&=]*)? 
利用正则表达式限制网页表单里的文本框输入内容: 
用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,&#39;&#39;)" 
用正则表达式限制只能输入全角字符: 
用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^d]/g,&#39;&#39;) "onbeforepaste="clipboardData.setData 
(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^d]/g,&#39;&#39;))" 
用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[W]/g,&#39;&#39;) "onbeforepaste="clipboardData.setData 
(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^d]/g,&#39;&#39;))" 
=========常用正则式 

匹配中文字符的正则表达式: [\x{4e00}-\x{9fa5}]
匹配双字节字符(包括汉字在内):[^\x00-\xff] 
匹配空行的正则表达式:\n[\s| ]*\r 
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 
匹配首尾空格的正则表达式:(^\s*)|(\s*$) 
匹配IP地址的正则表达式:/(\d+)\.(\d+)\.(\d+)\.(\d+)/g // 
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
匹配网址URL的正则表达式:[url=http://(/[\w-]+\.)+[\w-]+(/[\w]http://(/[\w-]+\.)+[\w-]+(/[\w[/url]- ./?%&=]*)? 
sql语句:^(select|drop|delete|create|update|insert).*$ 
1、非负整数:^\d+$ 
2、正整数:^[0-9]*[1-9][0-9]*$ 
3、非正整数:^((-\d+)|(0+))$ 
4、负整数:^-[0-9]*[1-9][0-9]*$ 
5、整数:^-?\d+$ 
6、非负浮点数:^\d+(\.\d+)?$ 
7、正浮点数:^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ 
8、非正浮点数:^((-\d+\.\d+)?)|(0+(\.0+)?))$ 
9、负浮点数:^(-((正浮点数正则式)))$ 
10、英文字符串:^[A-Za-z]+$ 
11、英文大写串:^[A-Z]+$ 
12、英文小写串:^[a-z]+$ 
13、英文字符数字串:^[A-Za-z0-9]+$ 
14、英数字加下划线串:^\w+$ 
15、E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ 
16、URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$ 
或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\&#39;:+!]*([^<>\"\"])*$ 
17、邮政编码:^[1-9]\d{5}$ 
18、中文:^[\u0391-\uFFE5]+$ 
19、电话号码:^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$ 
20、手机号码:^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$ 
21、双字节字符(包括汉字在内):^\x00-\xff 
22、匹配首尾空格:(^\s*)|(\s*$)(像vbscript那样的trim函数) 
23、匹配HTML标记:<(.*)>.*<\/\1>|<(.*) \/> 
24、匹配空行:\n[\s| ]*\r 
25、提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *(&#39;|")?(\w|\\|\/|\.)+(&#39;|"| *|>)? 
26、提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
27、提取信息中的图片链接:(s|S)(r|R)(c|C) *= *(&#39;|")?(\w|\\|\/|\.)+(&#39;|"| *|>)? 
28、提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+) 
29、提取信息中的中国手机号码:(86)*0*13\d{9} 
30、提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8} 
31、提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14} 
32、提取信息中的中国邮政编码:[1-9]{1}(\d+){5} 
33、提取信息中的浮点数(即小数):(-?\d*)\.?\d+ 
34、提取信息中的任何数字 :(-?\d*)(\.\d+)? 
35、IP:(\d+)\.(\d+)\.(\d+)\.(\d+) 
36、电话区号:/^0\d{2,3}$/ 
37、腾讯QQ号:^[1-9]*[1-9][0-9]*$ 
38、帐号(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 
39、中文、英文、数字及下划线:^[\u4e00-\u9fa5_a-zA-Z0-9]+$

 相关推荐:

PHP最常用的正则表达式的详解

PHP正则表达式合集

The above is the detailed content of What are PHP regular expressions? How to use PHP regular expressions (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Debunking the Myths: Is PHP Really a Dead Language?Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AM

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

The PHP vs. Python Debate: Which is Better?The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor