有没有办法在 preg_replace_callback 回调函数中传递另一个参数?
嗯,伙计们,我真的希望我的英语足够好来解释我需要什么。
让我们以这个代码示例(这只是一个示例!)为例:
class Something(){
public function Lower($string){
return strtolower($string);
}
}
class Foo{
public $something;
public $reg;
public $string;
public function __construct($reg, $string, $something){
$this->something = $something;
$this->reg = $reg;
$this->string = $string;
}
public function Replace(){
return preg_replace_callback($this->reg, 'Foo::Bar', $this->string);
}
public static function Bar($matches){
/*
* [...]
* do something with $matches and create the $output variable
* [...]
*/
/*
* I know is really useless in this example, but i need to have an istance to an object here
* (in this example, the Something object, but can be something else!)
*/
return $this->something->Lower($output);
}
}
$s = new Something();
$foo = new Foo($myregexp, $mystring, $s);
$content = $foo->Replace();
因此,php 手册说要在 preg_replace_callback()
中使用类方法作为回调,该方法一定是抽象的。
我需要在回调函数中传递先前初始化的对象的实例(在示例中为 Something
类的实例)。
我尝试使用 call_user_func ()
,但不起作用(因为这样我就错过了 matches
参数)。
有没有办法做到这一点,或者让我分开这个过程(在 preg_match_all
之前执行,对于每个匹配检索替换值,然后是一个简单的 preg_replace
)?
编辑:作为旁注,在汤姆海格回答之前,我使用了这个解决方法(在示例中,这是替换方法):
$has_dynamic = preg_match_all($this->reg, $this->string, $dynamic);
if($has_dynamic){
/*
* The 'usefull' subset of my regexp is the third, so $dynamic[2]
*/
foreach($dynamic[2] AS $key => $value){
$dynamic['replaces'][$key] = $this->Bar($value);
}
/*
* ..but i need to replace the complete subset, so $dynamic[0]
*/
return str_replace($dynamic[0], $dynamic['replaces'], $this->string);
}else{
return $this->string;
}
希望可以帮助别人。
mmmh guys, i really hope my english is good enaught to explain what i need.
Lets take this example (that is just an example!) of code:
class Something(){
public function Lower($string){
return strtolower($string);
}
}
class Foo{
public $something;
public $reg;
public $string;
public function __construct($reg, $string, $something){
$this->something = $something;
$this->reg = $reg;
$this->string = $string;
}
public function Replace(){
return preg_replace_callback($this->reg, 'Foo::Bar', $this->string);
}
public static function Bar($matches){
/*
* [...]
* do something with $matches and create the $output variable
* [...]
*/
/*
* I know is really useless in this example, but i need to have an istance to an object here
* (in this example, the Something object, but can be something else!)
*/
return $this->something->Lower($output);
}
}
$s = new Something();
$foo = new Foo($myregexp, $mystring, $s);
$content = $foo->Replace();
So, the php manual say that to use a class method as callback in preg_replace_callback()
, the method must be abstract.
I need to pass an instance of a previuosly initialized object (in the example, an instance of the Something
class) at the callback function.
I tryed to use call_user_func()
, but doesnt work (becose in this way i miss the matches
parameter).
Is there a way to do that, or have i to separate the process (doing before preg_match_all
, for each match retrieve the replace value, and then a simple preg_replace
)?
edit: as a side-note, before the tom haigh answer, i used this work-around (in the example, this is the Replace method):
$has_dynamic = preg_match_all($this->reg, $this->string, $dynamic);
if($has_dynamic){
/*
* The 'usefull' subset of my regexp is the third, so $dynamic[2]
*/
foreach($dynamic[2] AS $key => $value){
$dynamic['replaces'][$key] = $this->Bar($value);
}
/*
* ..but i need to replace the complete subset, so $dynamic[0]
*/
return str_replace($dynamic[0], $dynamic['replaces'], $this->string);
}else{
return $this->string;
}
Hope can help someone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很难将参数传递给回调,但可以使用以下方法代替:
您可以使
Bar()
不是静态的,并使用它:然后回调函数将能够看到
$this
请参阅 伪类型和变量 中的“回调”
也在 PHP 中>=5.3 你可以使用 匿名函数/闭包 将其他数据传递给回调功能。
It is hard to pass arguments to callbacks, but instead of this:
You could make
Bar()
not static, and use this:Then the callback function will be able to see
$this
See 'callback' in Pseudo-types and variables
Also in PHP >=5.3 you could use anonymous functions/closures to pass other data to callback functions.
我在尝试将参数(额外参数)传递给回调时陷入困境
使用 create_function() 和 call_user_function() 方法。
这是供参考的:
I got stuck while trying to pass an argument (extra parameter) to a callback
with the create_function() and call_user_function() methods.
This is for reference:
是的,我使用类似这样的方法来设置和取消设置更改变量,以便回调函数可以使用它,并且您不需要较新的 php 来执行此操作:
然后在回调函数中 $this->_current_key 可用:
yes I use something like this to set and unset a changing variable so that it is available to the callback function and you don't need the newer php to do it:
then in the callback function $this->_current_key is available: