呃...我有一个类,试图制作一个模板类,使用preg_replace_callback,但我不知道参数2如何写
class template {
public function parse_template($newtpl, $cachetpl){
......
$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
$template = preg_replace_callback("/\{lang\s+(.+?)\}/is", $this->languagevar('\\1'), $template);
......
}
public function languagevar($param1){
......
return $lang[$param1];
......
}
}
这是如何工作的?
在我的 html(模板)文件中,有类似 {lang hello} 的内容,parse_template 找到了 {lang everything} 它将使用 $this->languagevar('hello'); 进行转换
但我不断收到错误
$template = preg_replace_callback("/\{lang\s+(.+?)\}/is", $this->languagevar('\\1'), $template);
错误消息是
preg_replace_callback(): Requires argument 2, '('\1')', to be a valid callback
在我可以使用
进行回调之前preg_replace("/\{lang\s+(.+?)\}/ies", "\$this->languagevar('\\1')", $template);
但也许当前的 php 版本问题,它收到错误说/e 已弃用,请改用 preg_replace_callback
最佳答案
用途:
$template = preg_replace_callback("/\{lang\s+(.+?)\}/is", array($this, 'languagevar'), $template);
传递给回调函数的参数是一个匹配元素的数组,因此您必须将 languagevar
更改为以下内容:
public function languagevar($matches){
/// ......
return $lang[$matches[1]];
}
关于php - preg_replace_callback 在参数 2 处调用 $this->method ('\\1' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227923/