CakeFest 2024: The Official CakePHP Conference

注释

字符序列(?#标记开始一个注释直到遇到一个右括号。不允许嵌套括号。 注释中的字符不会作为模式的一部分参与匹配。

如果设置了 PCRE_EXTENDED 选项, 一个字符类外部的未转义的 # 字符就代表本行剩余部分为注释。

示例 #1 PCRE 模式中注释的用法

<?php

$subject
= 'test';

/* (?# 可用于添加注释而不启用 PCRE_EXTENDED */
$match = preg_match('/te(?# this is a comment)st/', $subject);
var_dump($match);

/* 除非启用 PCRE_EXTENDED,否则空格和 # 视为模式的一部分 */
$match = preg_match('/te #~~~~
st/'
, $subject);
var_dump($match);

/* 启用 PCRE_EXTENDED 后,所有空白数据字符以及
同一行未转义的 # 后面的任何内容都将会忽略 */
$match = preg_match('/te #~~~~
st/x'
, $subject);
var_dump($match);

以上示例会输出:

int(1)
int(0)
int(1)

add a note

User Contributed Notes 1 note

up
-13
asamaru at asamaru dot net
7 years ago
<?php
$string
= 'test';
echo
preg_match('/te(?# comments)st/', $string) . "\n";
echo
preg_match('/te#~~~~
st/'
, $string) . "\n";
echo
preg_match('/te#~~~~
st/x'
, $string) . "\n";

// result
// 1
// 0
// 1
To Top