Refencing preg_replace_callback for functions inside classes

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
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Refencing preg_replace_callback for functions inside classes

Post by Darkzaelus »

Hiya, just wondering how i could reference a function for preg_replace_callback like this:

Code: Select all

 
<?php
class testclass {
   function match($matches) {
   ...
   }
   function replace() {
   preg_replace_callback(...);
   }
}
?>
 
where the preg_replace_callback would reference the function match.

Cheers, Darkzaelus
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: Refencing preg_replace_callback for functions inside classes

Post by dml »

I think you can reference instance methods with array($object, $functionname).

Code: Select all

 
class testclass {
    function match($matches) {
    ...
    }
    function replace() {
    preg_replace_callback("/.../", array($this, "match"), "...")
    }
 }
 
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Refencing preg_replace_callback for functions inside classes

Post by Darkzaelus »

You are an absolute genius!!!
Major kudos.

Cheers!!!

Darkzaelus
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Refencing preg_replace_callback for functions inside classes

Post by Darkzaelus »

Sorry, this doesn't seem to work. I used

Code: Select all

 
$arr=preg_replace_callback('/\\{.*?\\}/is',array($this,'match'),$arr);
 
where the function match($matches) is

Code: Select all

 
public function match($matches) {
    echo 'true';
}
 
Any ideas why?

I plan on using this for my templating engine, to escape single quotes only inside {literal}...{/literal} tags. I have the REGEX, just need to make this work.

Thanks a lot, Darkzaelus
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: Refencing preg_replace_callback for functions inside classes

Post by dml »

No idea. Can you post some runnable test code, or at least give us some sample input together with the expected result of the preg_replace?
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Refencing preg_replace_callback for functions inside classes

Post by Darkzaelus »

Code: Select all

 
class PseudoView{
             public function match($matches) {
        echo 'true';
    }
             public function test() {
                          $arr='{test}';
                 $arr=preg_replace_callback('/\\{(.*?)\\}/is',array($this,'match'),$arr);
             }
}
 
Should it not echo true?
Cheers, Darkzaelus
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Refencing preg_replace_callback for functions inside classes

Post by Darkzaelus »

Also, checked the error.log in the apache directory, nothing there, apart from the fact that my personal site got pinged by some guy in china looking for http://mywebsite/manager. This is strange, since my personal site isnt on any search engine i know of...

Cheers, Darkzaelus
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Refencing preg_replace_callback for functions inside classes

Post by Darkzaelus »

Right! I have found out how to reference a function within a class in a callback!
I got the idea from http://uk.php.net/manual/en/language.ps ... s.callback.

Code: Select all

$callback=array(($this|'parent class'), 'parent_class_name::function');
Thanks for everyone's help!
Cheers, Darkzaelus
Post Reply