Replace with Dynamic variable in preg_replace

  • MarPlo
  • Novice
  • Novice
  • No Avatar
  • Joined: Feb 16, 2012
  • Posts: 34
  • Status: Offline

Post February 19th, 2012, 11:50 pm

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 19th, 2012, 11:50 pm

  • MarPlo
  • Novice
  • Novice
  • No Avatar
  • Joined: Feb 16, 2012
  • Posts: 34
  • Status: Offline

Post February 20th, 2012, 7:30 am

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);
Free PHP-MySQL Course and video Lessons.
Good JavaScript and jQuery course for beginners.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 232 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
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.