note 110353 added to function.preg-replace-callback

From: Date: Sun, 14 Oct 2012 22:27:26 +0000
Subject: note 110353 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-191290@lists.php.net to get a copy of this message
If you want to call non-static function inside your class, you can do something like this. For PHP 5.2 use second argument like array($this, 'replace'): <?php class test_preg_callback{ private function process($text){ $reg = "/\{([0-9a-zA-Z\- ]+)\:([0-9a-zA-Z\- ]+):?\}/"; return preg_replace_callback($reg, array($this, 'replace'), $text); } private function replace($matches){ if (method_exists($this, $matches[1])){ return @$this->$matches[1]($matches[2]); } } } ?> For PHP 5.3 use second argument like "self::replace": <?php class test_preg_callback{ private function process($text){ $reg = "/\{([0-9a-zA-Z\- ]+)\:([0-9a-zA-Z\- ]+):?\}/"; return preg_replace_callback($reg, "self::replace", $text); } private function replace($matches){ if (method_exists($this, $matches[1])){ return @$this->$matches[1]($matches[2]); } } } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 94.228.254.82 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- https://master.php.net/note/edit/110353 Del: integrated -- https://master.php.net/note/delete/110353/integrated Del: useless -- https://master.php.net/note/delete/110353/useless Del: bad code -- https://master.php.net/note/delete/110353/bad+code Del: spam -- https://master.php.net/note/delete/110353/spam Del: non-english -- https://master.php.net/note/delete/110353/non-english Del: in docs -- https://master.php.net/note/delete/110353/in+docs Del: other reasons-- https://master.php.net/note/delete/110353 Reject -- https://master.php.net/note/reject/110353 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191290) next »