note 109074 modified in function.preg-replace-callback by danbrown
| From: | danbrown@php.net | Date: | Tue, 19 Jun 2012 13:45:24 +0000 |
| Subject: | note 109074 modified in function.preg-replace-callback by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188693@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']; } ?>
--was--
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'];
}
http://php.net/manual/en/function.preg-replace-callback.php