preg_replace_callback does not return values

Asked

Viewed 45 times

0

That my code is not returning the values $owner and $mid in function make_user() Because?

preg_replace_callback('/(^|[^a-z0-9_])M([a-z0-9_]+)/i', function($matches, $owner, $mid) {
    return $this->mark_user($matches, $owner, $mid);
}, $text);
  • add a larger context to this code

  • Because this would not be the right way to pass these values, where is the $Owner statement and $mid?

  • 1

    From what I saw, you tried to create an anonymous function (lambda style) and within it you called another function, which makes no sense, you could have referenced the function itself. You need to reference the function like this: Function($Matches) use($Owner, $mid) etc...

1 answer

1


Example of the function (Since the user has not posted the whole context of the question):

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']; 
} 

source: http://www.php.net/manual/en/function.preg-replace-callback.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.