0

I've had a good look through all the previous topics and I don't understand enough PHP to use them to answer my questions so sorry in advance if this is really simple!

{
    $content = preg_replace('/\$([\w]+)/e', '$0', $this->getTemplateStyle());
    $custom_css = $this->getCustomCSS();
    return $content.$custom_css;
}

And I need to replace preg_replace with preg_replace_callback. I know it's not a simple switch and that I need to add more to the code, but I don't know what to add. Thanks in advance for your help.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
Claire
  • 1

1 Answers1

-1

[SOLVED] You seem to be trying to inject variables from the current scope into your string. I'll leave aside why this is a bad idea and assume there is no user input involved. First get the current scope:

$scope = get_defined_vars();

Next use the callback:

preg_replace_callback('/\$(\w+)/',function($m) use ($scope) {if( isset($scope[$m[1]])) return $scope[$m[1]]; else return $m[0];}, $this->getTemplateStyle()); 

Job done. – @Niet the Dark Absol

Foo Bar
  • 165
  • 2
  • 14
  • 1
    If it's a duplicate, why are you answering? – Niet the Dark Absol May 26 '17 at 10:45
  • I'm really grateful for the link to the other answer, but my php knowledge is too basic to translate how I can use it for my problem. I haven't got the same bit '/(\d+);/m' and I guess I'm trying to get it to call a style sheet, but I don't know what to change. I really need some code I can cut and paste - sorry to be so dumb! – Claire May 26 '17 at 11:01
  • 1
    You seem to be trying to inject variables from the current scope into your string. I'll leave aside why this is a bad idea and assume there is no user input involved. First get the current scope: `$scope = get_defined_vars();`. Next use the callback: `preg_replace_callback('/\$(\w+)/',function($m) use ($scope) {if( isset($scope[$m[1]])) return $scope[$m[1]]; else return $m[0];}, $this->getTemplateStyle());`. Job done. – Niet the Dark Absol May 26 '17 at 11:16
  • @NiettheDarkAbsol well done. – Foo Bar May 26 '17 at 11:19
  • 1
    Thank you @NiettheDarkAbsol and Foo Bar - eternally grateful! – Claire May 26 '17 at 11:24