note 74969 deleted from function.preg-replace-callback by felipe

From: Date: Sun, 23 Mar 2008 23:02:17 +0000
Subject: note 74969 deleted from function.preg-replace-callback by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137409@lists.php.net to get a copy of this message
Note Submitter: sjungwirth domain matrix-consultants com ---- one way to 'pass' extra info other than just the matches and still use preg_replace_callback is to have your callback function be a class method, and just before you call preg_replace_callback, store the info in that class. I am using static methods/variables for my implementation; I'm not sure if its required. <?php class foo { private static $bar; public static function do_something($bar) { self::$bar = $bar; // store bar $input = $bar->get_somevar(); $pattern = "match me"; $output = preg_replace_callback($pattern, array('self', 'do_something_callback'), $input); return $output; } private static function do_something_callback($matches) { $bar = self::$bar; // retrieve bar // do stuff like $bar->get_someothervar(); return $replacement; } } ?>

« previous php.notes (#137409) next »