Forum Moderators: coopster

Message Too Old, No Replies

Using an object method as callback in preg_replace_callback?

This seems to be impossible...

         

MTKilpatrick

12:59 am on Jul 23, 2003 (gmt 0)

10+ Year Member



Folks,

is it possible to call the method of a class object as the callback function of a preg_replace_callback(), if that preg_replace is taking place within another method? I can't seem to get it to work.

Usually you specify the function as string:

function to_mine($matches) { return $matches[1]."mine";}

preg_replace_callback("/This is (yours)/, "to_mine", $inputstring);

or something along the lines of the above.

But, what if you want to call this->to_mine, and you
are executing the preg_replace_callback in another method
within the class? You can't do this:

preg_replace_callback("/This is (yours)/, "$this->to_mine", $inputstring);

It seems to be impossible. Is there a way to do it?

thanks,

Michael

DrDoc

4:57 am on Jul 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't put $this->to_mine in quotes... It won't be parsed correctly. Only plain variables can be surrounded by quotes.

"$myvar" // works
"$_POST['myvar']" // doesn't work
"$myvar->foovar" // doesn't work either

vincevincevince

7:53 am on Jul 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the online manual for preg_replace:
/e modifier makes preg_replace() treat the replacement parameter as PHP code after the appropriate references substitution is done. Tip: make sure that replacement constitutes a valid PHP code string, otherwise PHP will complain about a parse error at the line containing preg_replace().

Tried that?

MTKilpatrick

10:12 am on Jul 23, 2003 (gmt 0)

10+ Year Member



Vince,

thanks for that. I'm struggling with the syntax of the encapsulated code for the preg_replace. I'm not sure if it will do what I want. Do you think you could take a look?

What I want to do is parse a string of this form: {@text1=text2}

using preg_replace, I want to find the two fields text1 and text2, and execute the following, assuming text is "score" and text2 is "hgll".

link_score("hgll","",1)

If text1 is "menu" and text2 is "item1", I want to execute this function call:

link_menu("item1","",1)

where the functions link_menu() and link_score() both return a string which is to be used as the replacement for the entire {@text1=text2} fragment.

I was trying this:

$result = preg_replace("/\{@([a-z]+)\=([a-z]+)\}/e", "link_\\1.'(\"'.'\\2'.'\",\"\",1)'", $inputstring);

I think I'm struggling with all these \" escape codes! So far it just replaces {@text1=text2} with the text of the code, 'link_score("hgll","",1)' without actually executing it!

Michael

MTKilpatrick

10:17 am on Jul 23, 2003 (gmt 0)

10+ Year Member



Oh! Don't worry, I think I've fixed it with:

preg_replace("/\{@([a-zA-Z0-9_]+)\=([a-zA-Z0-9_]+)\}/e", 'link_\\1("\\2","",1)', $string);

it should be encapsulated in single quote marks, by the look of it.

A very useful bit of functionality!

Michael