How can I pass a variable into the custom callback function of a preg_replace_callback()
call?
For example, I'd like to use the variable $add
within my callback function:
private function addToWord($add) {
return preg_replace_callback(
'/([a-z])+/i',
function($word, $add) {
return $word.$add;
},
$this->text
);
}
You can use use
keyword here:
private function addToWord($add) {
return preg_replace_callback(
'/([a-z])+/i',
function($word) use ($add) {
return $word[1] . $add;
},
$this->text);
}