note 109938 added to function.preg-replace-callback
| From: | Richard | Date: | Tue, 04 Sep 2012 10:59:14 +0000 |
| Subject: | note 109938 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190476@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.
$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";
----
Server IP: 94.76.225.145
Probable Submitter: 87.194.171.29
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit -- https://master.php.net/note/edit/109938
Del: integrated -- https://master.php.net/note/delete/109938/integrated
Del: useless -- https://master.php.net/note/delete/109938/useless
Del: bad code -- https://master.php.net/note/delete/109938/bad+code
Del: spam -- https://master.php.net/note/delete/109938/spam
Del: non-english -- https://master.php.net/note/delete/109938/non-english
Del: in docs -- https://master.php.net/note/delete/109938/in+docs
Del: other reasons-- https://master.php.net/note/delete/109938
Reject -- https://master.php.net/note/reject/109938
Search -- https://master.php.net/manage/user-notes.php