$string = "Some numbers:one:1; Two:2; Three:3 end "; $ten = ten; $newstring = Preg_replace_callback ( '/(\\d+)/', function ($match) use ($ten) {return ($m Atch[0] + $ten)); }, $string
Reply to discussion (solution)
Use Chinese interpretation
function ($match) use ($ten) {return ($match [0] + $ten);}
Let the variable $ten be used in anonymous functions
Equivalent
$ten = 10;
function foo ($match) {
Global $ten;
Return (($match [0] + $ten));
}
But if $ten's not a global variable, it's going to be a problem.
New closure syntax for PHP 5.3
Closure functions (anonymous functions) can inherit variables from the parent scope any such variables should be passed in using the use language structure
Use Chinese interpretation
function ($match) use ($ten) {return ($match [0] + $ten);}
Let the variable $ten be used in anonymous functions
Equivalent
$ten = 10;
function foo ($match) {
Global $ten;
Return (($match [0] + $ten));
}
But if $ten's not a global variable, it's going to be a problem.
Can I replace it in 5.2?
Can't you do it if it's not a global variable? $ten;
New closure syntax for PHP 5.3
Closure functions (anonymous functions) can inherit variables from the parent scope any such variables should be passed in using the use language structure
5.2 Can't use it? Is there a replacement method?
5.2, you can do this.
$string = "Some numbers:one:1; Two:2; Three:3 end "; $ten = ten; $newstring = Preg_replace_callback ( '/(\\d+)/', create_function (' $match '," return \$ Match[0] + $ten; "), $string ); Echo $newstring;
5.2, you can do this.
$string = "Some numbers:one:1; Two:2; Three:3 end "; $ten = ten; $newstring = Preg_replace_callback ( '/(\\d+)/', create_function (' $match '," return \$ Match[0] + $ten; "), $string ); Echo $newstring;
Thank you