Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.