php_fish Posted December 30, 2008 Share Posted December 30, 2008 I've written a class which has a method that uses the php function preg_replace. For the $replacement parameter of this function I want it to be a class variable so have written the following code; $this->any_var = preg_replace($re,$this->{'$1'},$this->html_snippet); where $1 is the first bracketed group found by the regular expression which also happens to be the second part of the class variable, eg $this->class_var However it does not work, any ideas what I'm doing wrong? Your help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/138855-preg_replace/ Share on other sites More sharing options...
Adam Posted December 30, 2008 Share Posted December 30, 2008 Just use them as usual... $this->any_var = preg_replace($re, $1, $this->html_snippet); The $1, $2, etc. vars are created by the preg_replace function and do not require you to reference them as part of the class object ..Think I worded that right?? A Quote Link to comment https://forums.phpfreaks.com/topic/138855-preg_replace/#findComment-726087 Share on other sites More sharing options...
php_fish Posted December 30, 2008 Author Share Posted December 30, 2008 I need the $1 to produce the class variable name which in turn will be replaced by the data assigned to the class variable Quote Link to comment https://forums.phpfreaks.com/topic/138855-preg_replace/#findComment-726090 Share on other sites More sharing options...
rajivgonsalves Posted December 30, 2008 Share Posted December 30, 2008 can you post more code the code where you initialize $re ? Quote Link to comment https://forums.phpfreaks.com/topic/138855-preg_replace/#findComment-726092 Share on other sites More sharing options...
php_fish Posted December 30, 2008 Author Share Posted December 30, 2008 can you post more code the code where you initialize $re ? $re = "/<\?php\s*(\\$){1,1}(this\->){1,1}(\w*)\s*\?>/m"; not sure how that will help. It produces the correct result. The problem I am having is converting the string in $1 into a variable name see http://uk2.php.net/language.variables.variable for a detailed explanation. I just need the correct syntax to apply it to the preg_replace function or for someone to tell me it's not possible Quote Link to comment https://forums.phpfreaks.com/topic/138855-preg_replace/#findComment-726232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.