preg_replace_callback

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

preg_replace_callback

Post by kaisellgren »

Hi,

I didn't know how to put this in the title. I'm doing a preg_replace_callback to a function named callback_fix() within a class. Here's an example:

Code: Select all

class xxx
 {
  function do()
   {
    function callback_fix($matches)
     {
      return $this -> loader[$matches[1]];
     }
    preg_replace_callback(...,'callback_fix',...);
   }
 }
The problem is that I am using "$this ->" inside the callback function and therefore it will not work. I need to somehow refer to the $this. I know a few ways to do it, but I'm just wondering what's the best way to do it? (Fastest)
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: preg_replace_callback

Post by Syntac »

Something like this:

Code: Select all

preg_replace_callback(..., array($this, "method_name_here"), ...);
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: preg_replace_callback

Post by kaisellgren »

Syntac wrote:Something like this:

Code: Select all

preg_replace_callback(..., array($this, "method_name_here"), ...);
Thanks :)
Post Reply