Jump to content

preg_replace


php_fish

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/138855-preg_replace/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138855-preg_replace/#findComment-726087
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/138855-preg_replace/#findComment-726232
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.