note 109938 modified in function.preg-replace-callback by danbrown
| From: | danbrown@php.net | Date: | Tue, 04 Sep 2012 14:24:15 +0000 |
| Subject: | note 109938 modified in function.preg-replace-callback by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190483@lists.php.net to get a copy of this message | ||
The easiest way to pass more than one parameters to the callback function is with the
'use' keyword. [This is better than using
global, because it works even when we are already inside a
function.] In this example, the callback function is an anonymous
function, which takes one argument, $match, supplied by preg_replace_callback(). The extra
"use ($ten)" puts the $ten variable into scope for the
function. <?php $string = "Some
numbers: one: 1; two: 2; three: 3 end"; $ten =
10; $newstring =
preg_replace_callback( 	'/(\\d+)/', 	function($match)
use ($ten) { return (($match[0] + $ten));
}, 	$string 	); echo
$newstring; #prints "Some numbers: one: 11; two: 12; three: 13
end"; ?>
--was--
The easiest way to pass more than one parameters to the callback function is with the
'use' keyword.
[This is better than using global, because it works even when we are already inside a function.]
In this example, the callback function is an anonymous function, which takes one argument, $match,
supplied by preg_replace_callback(). The extra
"use ($ten)" puts the $ten variable into scope for the function.
$string = "Some numbers: one: 1; two: 2; three: 3 end";
$ten = 10;
$newstring = preg_replace_callback(
'/(\d+)/',
function($match) use ($ten) { return (($match[0] + $ten)); },
$string
);
echo $newstring;
#prints "Some numbers: one: 11; two: 12; three: 13 end";
http://php.net/manual/en/function.preg-replace-callback.php