Page 1 of 1

Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 12:13 pm
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

Re: Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 12:28 pm
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"), "...")
    }
 }
 

Re: Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 12:29 pm
by Darkzaelus
You are an absolute genius!!!
Major kudos.

Cheers!!!

Darkzaelus

Re: Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 12:38 pm
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

Re: Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 2:12 pm
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?

Re: Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 2:28 pm
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

Re: Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 2:40 pm
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

Re: Refencing preg_replace_callback for functions inside classes

Posted: Tue Sep 09, 2008 2:56 pm
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