BrettCarr Posted October 11, 2011 Share Posted October 11, 2011 Hi Guys, I have a small issue with doing a preg_replace on a string. Here is the string I wish to use preg_replace on <?php $mystring = "This is my test string that has {['site_info']} and some more stuff {['taste']} then nothing" ?> I want to do a preg_replace and replace {['site_info']} with $stuff['major']['site_info'] through the string I want to replace the placeholder with real variables from the array Any help would be great Link to comment https://forums.phpfreaks.com/topic/248865-preg_replace-problem/ Share on other sites More sharing options...
codefossa Posted October 11, 2011 Share Posted October 11, 2011 <?php $string = "This is my test string that has {['site_info']} and some more stuff {['taste']} then nothing"; $string = preg_replace('/\{\[\'site_info\'\]\}/', '$stuff[\'major\'][\'site_info\']', $string); echo $string; ?> Will echo this: This is my test string that has $stuff['major']['site_info'] and some more stuff {['taste']} then nothing But if you wanted to insert a variable rather than actually put the text in there like that: <?php $stuff['major']['site_info'] = 'VARIABLE!!'; $string = "This is my test string that has {['site_info']} and some more stuff {['taste']} then nothing"; $string = preg_replace('/\{\[\'site_info\'\]\}/', $stuff['major']['site_info'], $string); echo $string; ?> Will echo this: This is my test string that has VARIABLE!! and some more stuff {['taste']} then nothing Hope that answers your problem. Link to comment https://forums.phpfreaks.com/topic/248865-preg_replace-problem/#findComment-1278022 Share on other sites More sharing options...
BrettCarr Posted October 11, 2011 Author Share Posted October 11, 2011 Thanks Guys Awsome :-)) Link to comment https://forums.phpfreaks.com/topic/248865-preg_replace-problem/#findComment-1278041 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.