Home >Backend Development >PHP Tutorial >正则表达式 - PHP正则 #info/list#u 为什么反斜杠“/” 可以不用转义

正则表达式 - PHP正则 #info/list#u 为什么反斜杠“/” 可以不用转义

WBOY
WBOYOriginal
2016-06-06 20:33:441160browse

PHP 生成的正则,以 # 开头 #u结尾的,为什么里面的 / 反斜杠不用转义成 \/

<code>php</code><code><?php $pattern = '#^info/(?P<type>(area|cat))-(?P<v1>\d+)-(?P<v2>\d+)-(?P<v3>\d+)/(?P<page>\d+)/$#u';
$str = 'info/area-19-0-0/40/';
preg_match($pattern,$str,$matches);
print_r($matches);

</page></v3></v2></v1></code>
<code>Array
(
    [0] => info/area-19-0-0/40/
    [type] => area
    [1] => area
    [2] => area
    [v1] => 19
    [3] => 19
    [v2] => 0
    [4] => 0
    [v3] => 0
    [5] => 0
    [page] => 40
    [6] => 40
)
</code>

回复内容:

PHP 生成的正则,以 # 开头 #u结尾的,为什么里面的 / 反斜杠不用转义成 \/

<code>php</code><code><?php $pattern = '#^info/(?P<type>(area|cat))-(?P<v1>\d+)-(?P<v2>\d+)-(?P<v3>\d+)/(?P<page>\d+)/$#u';
$str = 'info/area-19-0-0/40/';
preg_match($pattern,$str,$matches);
print_r($matches);

</page></v3></v2></v1></code>
<code>Array
(
    [0] => info/area-19-0-0/40/
    [type] => area
    [1] => area
    [2] => area
    [v1] => 19
    [3] => 19
    [v2] => 0
    [4] => 0
    [v3] => 0
    [5] => 0
    [page] => 40
    [6] => 40
)
</code>

因为它的正则分隔符是 # 号, 所以 / 不需要进行转义.

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