In the previous article, I brought you "Learn to use PHP's List, each function and cooperation", which mainly explained how to use the list function and each function and what should be done between the two. When used together, I believe you have almost mastered it, so in this article we will take a look at regular expressions in PHP. I hope everyone has to help!
Regular expression is a logical formula, which is a "specified string" composed of some characters declared at the beginning and a combination of these characters. This The specified string is actually used to filter the string. It can be understood that when logging in as a user, we sometimes need to fill in specific data such as a verification code or phone number. At this time, we need to use regular expressions.
Although regular expressions look complicated, they are actually not difficult. Let’s take a look at them next.
The delimiters of regular expressions
#The first thing we have to learn is the delimiters of regular expressions. As the name suggests, The delimiter is a symbol that determines the boundary of a regular expression. Set a boundary, and the regular expression is within the boundary. At the same time, the delimiter of regular expressions is stipulated:
delimiter cannot be used, a-zA-Z0-9\ can be used, otherwise it can be used. And they must appear in pairs, with a beginning and an end.
The example is as follows:
$正则表达式$ %正则表达式% /正则表达式/
What we need to pay attention to is that /
is an escape character. When we need to match / in the regular expression, You can use \ to escape it. If you find it troublesome, you can directly use other delimiters such as:
$/$
The atoms of regular expressions
The atoms of regular expressions are regular expressions The smallest unit in the formula is what we need to match. In the integer expression we establish, there must be at least one atom.
In fact, it can be understood that all visible and invisible characters are atoms, such as spaces, carriage returns, line feeds, 0-9, punctuation marks, A-Za-z, and Chinese. These are atoms.
preg_match() function
Before talking about atoms in detail, we need to understand a function first, that ispreg_match
The preg_match() function in PHP can search and match strings based on defined regular expressions.
The syntax format is as follows:
preg_match ( string $正则 , string $字符串 [, array &$结果] )
According to $regex, which is the regular expression we defined, when matching $string, if it exists, the number of matches will be returned, and then the matching result will be placed in $In the results. If no result is found, 0 is returned.
Let’s take a look at it through an example:
<?php //定义一个变量叫a,作为我们定义的正则表达式。 $a = '/a/'; $b = 'abbcccddddeeeee'; if(preg_match($a, $b, $c)){ echo '匹配到了,结果为:'; var_dump($c); }else{ echo '没有匹配到'; } ?>
Output result:
As can be seen from the above example , we defined the variable a, hoping to match a, which happens to exist in $b, and the output through the if else statement was successful.
Another example:
<?php //定义一个变量叫a,作为我们定义的正则表达式。 $a = '/fff/'; $b = 'abbcccddddeeeee'; if(preg_match($a, $b, $c)){ echo '匹配到了,结果为:'; var_dump($c); }else{ echo '没有匹配到'; } ?>
Output result:
In the above example, we hope to match the string, but $ It does not exist in b, so there is no successful match. The if else statement outputs that the match is successful.
After knowing the basic usage of the preg_match() function, we can use it in combination with specially identified atoms.
Specially identified atoms
##\d---matches a 0-9
\D---All characters except 0-9
---a-zA- Z0-9_
<?php $a = '/\d/'; $b = '人生自古谁无4'; if(preg_match($a, $b, $c)){ echo '匹配到了,结果为:'; var_dump($c); }else{ echo '没有匹配到'; } ?>
Output result:
In the above example, the specially identified atom \d represents 0- If the number is 9, then there is a 4 in $b that needs to be matched, so the match is successful.
<?php $a = '/\w/'; $b = '人生自古谁无死'; if(preg_match($a, $b, $c)){ echo '匹配到了,结果为:'; var_dump($c); }else{ echo '没有匹配到'; } ?>
#In the above example, the specially identified atom \w represents a-zA-Z0-9_, There is no corresponding element in variable b, so the output result is not matched.
There is also a[^]
character indicating characters that do not match the specified range.The example is as follows:
<?php $a = '/[^0-9A-Za-z_]/'; $b = 'abbccc122333'; if(preg_match($a, $b, $c)){ echo '匹配到了,结果为:'; var_dump($c); }else{ echo '没有匹配到'; } ?>
通过[^]字符匹配除0-9A-Za-z_以外的字符,未匹配到。
总结一下:
\w
---[a-zA-Z0-9_]
\W
---[^a-zA-Z0-9_]
\d
---[0-9]
\D
---[^0-9]
\s
---[ \t\n\f\r]
\S
---[^ \t\n\f\r]
正则表达式的元字符
在上面的示例中,我们能够看出通过匹配的话,只能匹配一个字符,但是在我们的日常使用中,通常会匹配多个字符,那这时候只通过我们的原子就不能达到我们的目的。就需要通过元字符来帮我们修饰原子,实现更多的功能。
*
---代表匹配前面的一个原子,匹配0次或者任意多次前面的字符。
+
---匹配一次或多次前面的一个字符
?
---前面的字符可有可无【可选】 有或没有
.
---更标准一些应该把点算作原子。匹配除了\n以外的所有字符 或者。注:它的优先级最低了。
^
---必须要以抑扬符之后的字符串开始
$
--- 必须要以$之前的字符结尾
\b
---词边界
\B
---非边界
{m}
---有且只能出现m次
{n,m}
---可以出现n到m次
{m,}
---至少m次,最大次数不限制
()
---改变优先级或者将某个字符串视为一个整体,匹配到的数据取出来也可以使用它
接下来我们通过一些例子来实例看一下这些元字符的使用:
<?php $a = '/\d+/'; $b = "爱你10000年"; if(preg_match($a, $b, $c)){ echo '匹配到了,结果为:'; var_dump($c); }else{ echo '没有匹配到'; } ?>
输出结果:
通过元字符+的添加,匹配到了多次字符,\d+中d是匹配数字,+则表示最少匹配一次前面的字符。
正则表达式的模式修正符
通过原子和元字符的了解,我们已经完成了正则表达式的入门,但是这仍然不能代表正则表达式的真正实力,如果我们只希望正则表达式匹配一部分应该怎么办?有些特殊情况依然需要处理,这时候我们就要用到正则表达式的模式修正符。
下面列举一些常用的模式修正符:
i 模式中的字符将同时匹配大小写字母.
m 字符串视为多行
s 将字符串视为单行,换行符作为普通字符.
x 将模式中的空白忽略.
A 强制仅从目标字符串的开头开始匹配.
D 模式中的美元元字符仅匹配目标字符串的结尾.
U 匹配最近的字符串.
它的用法如下:
/正则表达式/模式修正符
接下来我们通过一些实例来看一下它的使用:
<?php $a = '/ABC/i'; $b = '8988abc12313'; $c = '11111ABC2222'; if(preg_match($a, $b, $d)){ echo '匹配到了,结果为:'; var_dump($d); }else{ echo '没有匹配到'; } ?>
输出结果:
i可以让匹配的时候同时匹配大小写,那么接下来把匹配的$b换成$c试一下,我们看一下输出结果:
<?php $a = '/ABC/i'; $b = '8988abc12313'; $c = '11111ABC2222'; if(preg_match($a, $c, $d)){ echo '匹配到了,结果为:'; var_dump($d); }else{ echo '没有匹配到'; } ?>
输出结果:
推荐学习:《PHP视频教程》
The above is the detailed content of The exciting basics of regular expressions in PHP (detailed explanation with illustrations). For more information, please follow other related articles on the PHP Chinese website!