I have a replace callback method where I am trying to replace multiple string occurrences with array values accordingly.
我有一个替换回调方法,我尝试用数组值替换多个字符串。
I have passed the $parametersArray
to the callback method through the use
keyword as follows (the regex matches 3 elements):
我已经通过use关键字(regex匹配3个元素)将$parametersArray传递给回调方法:
$string = 'Welcome Mr MM1, MM2 MM3 to the website';
$parametersArray = array('K', 'A' , 'AD');
$line = preg_replace_callback(
'(MM[1-9])',
// anonymous
function () use ($parametersArray) {
static $paramArray = $parametersArray;
return array_shift($paramArray);
},
$string
);
I am getting the below error:
我得到了以下错误:
Parse error: syntax error, unexpected '$parametersArray' (T_VARIABLE)
语法错误,意外的'$parametersArray' (T_VARIABLE)
If I set the array to the static variable explicitly, I do not get the error and get the expected behavior.
如果我显式地将数组设置为静态变量,就不会得到错误并得到预期的行为。
Is there a problem with assigning the array as variable directly to the statically defined variable within the function?
是否存在将数组作为变量直接分配给函数中静态定义的变量的问题?
3 个解决方案
#1
1
You can init static variable like this
可以像这样初始化静态变量
$line = preg_replace_callback(
'(MM[1-9]{1})',
// anonymous
function ($matches) use ($parametersArray) {
static $paramArray;
if (null === $paramArray) {
$paramArray = $parametersArray;
}
return array_shift($paramArray);
},
$string
);
#2
1
As per the docs (See example #6): You cannot initialize a static variable with an expression:
根据文档(参见示例#6):不能用表达式初始化静态变量:
<?php
function foo(){
static $int = 0; // correct
static $int = 1+2; // wrong (as it is an expression)
static $int = sqrt(121); // wrong (as it is an expression too)
$int++;
echo $int;
}
#3
0
You can reference the array and use it original values in the closure afterwards. Note that the parameters get initialized at definition not when you call it - so this might work for you:
您可以引用数组,然后在闭包中使用它的原始值。注意,参数在定义时被初始化,而不是当您调用它时——因此这可能对您有用:
<?php
$string = 'Welcome Mr MM1, MM2 MM3 to the website';
$parametersArray = array('K', 'A' , 'AD');
$refArray =& $parametersArray;
$line = preg_replace_callback(
'(MM[1-9]{1})',
// anonymous
function () use (&$refArray, $parametersArray) {
# stays the same in every call
# it is a copy after all
print_r($parametersArray);
return array_shift($refArray);
},
$string
);
echo $line;
# Welcome Mr K, A AD to the website
?>
#1
1
You can init static variable like this
可以像这样初始化静态变量
$line = preg_replace_callback(
'(MM[1-9]{1})',
// anonymous
function ($matches) use ($parametersArray) {
static $paramArray;
if (null === $paramArray) {
$paramArray = $parametersArray;
}
return array_shift($paramArray);
},
$string
);
#2
1
As per the docs (See example #6): You cannot initialize a static variable with an expression:
根据文档(参见示例#6):不能用表达式初始化静态变量:
<?php
function foo(){
static $int = 0; // correct
static $int = 1+2; // wrong (as it is an expression)
static $int = sqrt(121); // wrong (as it is an expression too)
$int++;
echo $int;
}
#3
0
You can reference the array and use it original values in the closure afterwards. Note that the parameters get initialized at definition not when you call it - so this might work for you:
您可以引用数组,然后在闭包中使用它的原始值。注意,参数在定义时被初始化,而不是当您调用它时——因此这可能对您有用:
<?php
$string = 'Welcome Mr MM1, MM2 MM3 to the website';
$parametersArray = array('K', 'A' , 'AD');
$refArray =& $parametersArray;
$line = preg_replace_callback(
'(MM[1-9]{1})',
// anonymous
function () use (&$refArray, $parametersArray) {
# stays the same in every call
# it is a copy after all
print_r($parametersArray);
return array_shift($refArray);
},
$string
);
echo $line;
# Welcome Mr K, A AD to the website
?>