didgydont Posted February 8, 2012 Share Posted February 8, 2012 Hi all This probarly looks really bad but im trying to replase tags that change. So far it only replaces the last tag and not well either. I will try to show you function testfunc($value) { if ($value=='gallery'){$return .= "this gallery has worked = --$value--";} if ($value=='email'){$return .= "this email has worked = --$value--";} return $return; } $test = "this is a test {%email%} message {%gallery%} rtfgdsfgdsfg"; $test = preg_replace("#(.*){%(.*?)%}(.*)#is",'$1' . testfunc('$2') . '$3', $test); echo $test; this outputs this is a test {%email%} message rtfgdsfgdsfg but im trying to get this this is a test this email has worked = --email-- message this gallery has worked = --gallery-- rtfgdsfgdsfg Im sure im way off but I hope you can understand. Thank you for taking the time to read this post. Quote Link to comment https://forums.phpfreaks.com/topic/256646-need-help-with-dynamic-tags/ Share on other sites More sharing options...
MasterACE14 Posted February 8, 2012 Share Posted February 8, 2012 are you after something like... <?php function parsetemplate($template, $array) { // Replace template with proper content. foreach($array as $a => $b) { $template = str_replace("{%{$a}%}", $b, $template); } return $template; } $test = "this is a test {%email%} message {%gallery%} rtfgdsfgdsfg"; $content['email'] = '[email protected]'; $content['gallery'] = 'random'; echo parsetemplate($test, $content); ?> Quote Link to comment https://forums.phpfreaks.com/topic/256646-need-help-with-dynamic-tags/#findComment-1315655 Share on other sites More sharing options...
didgydont Posted February 13, 2012 Author Share Posted February 13, 2012 Yes yes it is. Thank you very much had been trying to ages to work it out. For such small and tidy funtction its difficult to get whats going on :-\. Once againg thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/256646-need-help-with-dynamic-tags/#findComment-1317383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.