使用preg_replace_callback
,是否可以只替换一个反向引用?还是我必须退回整个东西?
我只是想用引号包装令牌的默认值。
$str = 'This is a {$token|token was empty}';
$str = preg_replace_callback('~{'$'w+'|(.*)?}~i', function($match) {
//$match[1] is "token was empty"
//I want to just replace $match[1], but it needs me to return the whole thing
}, $str);
我是否必须获取更多的反向引用,以便我能够构建新版本的令牌并返回它,我不能只替换反向引用 1?谢谢。
我是否必须获取更多的反向引用,以便我能够构建新版本的令牌并返回它,我不能只替换反向引用 1?
您有两种选择:
- 如您所说,使用额外的反向引用来构造替换字符串,或者
- 使用环顾仅匹配要替换的部件。
通常我建议使用第一种方法,因为第二种方法效率较低,并且在某些情况下可能导致无效匹配(当前瞻和后瞻可能重叠时)。在这种情况下,就不会有问题了。
第二个选项的示例是:
preg_replace_callback('~{'$'w+'|'K(?:[^{}]+)?(?=})~i', function($match){
// $match[0] contains what used to be the first capturing group.
// return the value you want to replace it with
// (you can still use the capturing group if you want, but it's unnecessary)
});
-
'K
是一种从实际匹配中排除之前的所有内容的方法(就像我们在那里有一个可变长度的回溯)。 -
(?=})
是一个展望,说以下内容必须是}
但不包括在匹配中。
您需要使用这样的正则表达式:
~'{'$('w+?)(?:'|(.+?))?'}~i
然后,您可以轻松查看传递给回调的内容:
$str = 'This is a {$token|token was empty}';
$str = preg_replace_callback('~'{'$('w+?)(?:'|(.+?))?'}~i', function($match) {
var_dump($match);
exit;
}, $str);
输出:
array(3) {
[0]=>
string(24) "{$token|token was empty}"
[1]=>
string(5) "token"
[2]=>
string(15) "token was empty"
}
从那里,您可以检查是否设置了$match[1]
,如果是,则返回其值,否则返回$match[2]
:
$foo = 'foo';
$str = 'Foo: {$foo|not set}, Bar: {$bar|not set}';
$str = preg_replace_callback('~'{'$('w+?)(?:'|(.+?))?'}~i', function($match) {
if (isset($GLOBALS[$match[1]])) {
return $GLOBALS[$match[1]];
} else {
return $match[2];
}
}, $str);
var_dump($str);
输出:
string(22) "Foo: foo, Bar: not set"
注意:我在这里使用$GLOBALS
仅用于演示目的。 如果可能的话,我建议使用 PHP 5.4 的闭包绑定,因为这样您就可以为闭包分配一个特定的对象作为上下文(例如,您的模板/视图对象或任何包含您尝试替换的变量)。 如果您没有使用 PHP 5.4,您还可以使用语法function($match) use ($obj)
,其中$obj
是您的上下文,然后在闭包中检查isset($obj->{$match[1]})
。
我最近想出了一种更简单的方法。例如;如果我想匹配'w+'d+'w+
并且只更改数字。
$value = preg_replace_callback('~('w+)('d+)('w+)~', function($match) {
$match[2] = $match[2] * 2;//Do whatever I want to $match[2]
return $match[1] . $match[2] . $match[3];
}, $value);
很干净!