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

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

WBOY
Release: 2016-06-06 20:33:44
Original
1167 people have browsed it

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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

回复内容:

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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template