Replace with Dynamic variable in preg_replace

  • MarPlo
  • Novice
  • Novice
  • MarPlo
  • Posts: 34

Post 3+ Months Ago

Hi
I'm trying the following code:
Code: [ Select ]
$t = '12<-- AB_C -->';
$AB_C = 'abc';
echo preg_replace('/\<-- ([A-Z_]+) --\>/', "$$1", $t);
  1. $t = '12<-- AB_C -->';
  2. $AB_C = 'abc';
  3. echo preg_replace('/\<-- ([A-Z_]+) --\>/', "$$1", $t);

I want to get "12abc" , but it outputs: 12$AB_C , so, it not recognize the replacement as dynamic variable.
Is it any way to use the matched word in preg_replace() as a variable, or dynamic variable?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • MarPlo
  • Novice
  • Novice
  • MarPlo
  • Posts: 34

Post 3+ Months Ago

For those who look for a solution to this problem, the '/e' flag, which evalates the replacement, solved the problem, and returns the results i want, using:
Code: [ Select ]
preg_replace('/\<-- ([A-Z_]+) --\>/e', "$$1", $t);

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 78 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.