Forum Moderators: coopster
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
/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().
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