note 109074 added to function.preg-replace-callback
| From: | development at HashNotAdam dot com | Date: | Tue, 19 Jun 2012 05:48:09 +0000 |
| Subject: | note 109074 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188683@lists.php.net to get a copy of this message | ||
From PHP 5.3 you can use an anonymous function to pass local variables into the callback.
<?php
public function replace_variables( $subject, $otherVars ) {
$linkPatterns = array(
'/(<a .*)href=(")([^"]*)"([^>]*)>/U',
"/(<a .*)href=(')([^']*)'([^>]*)>/U"
);
$callback = function( $matches ) use ( $otherVars ) {
$this->replace_callback($matches, $otherVars);
};
return preg_replace_callback($this->patterns, $callback, $subject);
}
public function replace_callback($matches, $otherVars) {
return $matches[1] . $otherVars['myVar'];
}
----
Server IP: 113.20.13.217
Probable Submitter: 60.240.128.121
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit -- https://master.php.net/note/edit/109074
Del: integrated -- https://master.php.net/note/delete/109074/integrated
Del: useless -- https://master.php.net/note/delete/109074/useless
Del: bad code -- https://master.php.net/note/delete/109074/bad+code
Del: spam -- https://master.php.net/note/delete/109074/spam
Del: non-english -- https://master.php.net/note/delete/109074/non-english
Del: in docs -- https://master.php.net/note/delete/109074/in+docs
Del: other reasons-- https://master.php.net/note/delete/109074
Reject -- https://master.php.net/note/reject/109074
Search -- https://master.php.net/manage/user-notes.php