MarPlo Posted February 20, 2012 Share Posted February 20, 2012 Hi I'm trying the following code: $t = '12<-- AB_C -->'; $AB_C = 'abc'; 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? Link to comment https://forums.phpfreaks.com/topic/257355-replace-with-dynamic-variable-in-preg_replace/ Share on other sites More sharing options...
requinix Posted February 20, 2012 Share Posted February 20, 2012 The easiest change would be to use the /e flag. Causes preg_replace() to evaluate the replacement string (after substitutions) as PHP code rather than a literal string. Link to comment https://forums.phpfreaks.com/topic/257355-replace-with-dynamic-variable-in-preg_replace/#findComment-1319103 Share on other sites More sharing options...
MarPlo Posted February 20, 2012 Author Share Posted February 20, 2012 Thank you requinix The '/e' flag solved the problem, and returns the results i want, using: preg_replace('/\<-- ([A-Z_]+) --\>/e', "$$1", $t); Link to comment https://forums.phpfreaks.com/topic/257355-replace-with-dynamic-variable-in-preg_replace/#findComment-1319128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.