nuttycoder Posted April 24, 2009 Share Posted April 24, 2009 this should be simple but can't seem to see it. I need to join a variable name and a string into a single variable for str_replace to work I have variables been included by including files and I want to do a string replace that replaced the contents of [$plugin] with the contents of $output, in the external file I have my veribles like $blogOutput. So when referencing them I need to get the 'blog' bit which I can that's inside $plugin and also need to add 'Output' to the end of it in such as way that php still knows its a varible coming from the included file. Tried a few ways nothing working as yet here's my latest attempt: $plugin_file = "plugins/$plugin/$plugin.php"; include($plugin_file); $output = $plugin . 'Output'; $string = str_replace("[$plugin]", $output, $string); Can some one give me a slap and tell me what am doing wrong... Quote Link to comment https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/ Share on other sites More sharing options...
mattal999 Posted April 24, 2009 Share Posted April 24, 2009 *slap*. Post the code of your included file, with the variable names please. Quote Link to comment https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/#findComment-818447 Share on other sites More sharing options...
nuttycoder Posted April 24, 2009 Author Share Posted April 24, 2009 this is from the included file just just one var as a test <?php $blogOutput = "FROM THE BLOG"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/#findComment-818448 Share on other sites More sharing options...
mattal999 Posted April 24, 2009 Share Posted April 24, 2009 Well, currently, that script is trying to replace this: Find the text '[$plugin]' with '$output' in the variable $string. The variable $string is blank. Do you mean to replace the string $blogOutput? Quote Link to comment https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/#findComment-818450 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 Ok I dont quite Understand you but let me see if I got this straight. You want to take a string, and replace every occurence of the sub string [$plugin] with the variable output? tha seems easy enough $output = $plugin."Output";//this is what you want to do right? $plugin = "[".$plugin."]";//this is what plugin should be right? what you want to replace? $string = str_replace($plugin, $output, $string); try that. I hope that helps. It is kind of difficult to understand what you are trying to do Quote Link to comment https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/#findComment-818451 Share on other sites More sharing options...
nuttycoder Posted April 24, 2009 Author Share Posted April 24, 2009 thanks for the reply guys think my post wasn't very clear let me try and explain it a bit better I have 2 files one called sample.php and one called blog,php that get included inside a function that is passed a block of text I want every reference of say [blog] to be replaced with $blogOutput and the same for [sample] and $sampleOutput <?php function outputPageCont($string) { $plugins = array('sample','blog'); foreach ($plugins as $plugin) { $plugin_file = "plugins/$plugin/$plugin.php"; include($plugin_file); $output = $plugin . 'Output'; $string = str_replace("[$plugin]", $output , $string); } echo $string; } here's the two files first blog.php: <?php $blogOutput = "FROM THE BLOG"; ?> and sample.php: <?php $sample = 'I come from the sample plugin'; ?> If I hand coded the names manually it would work put I'd like to do it dynamically <?php $string = str_replace("[$plugin]", $blogOutput, $string); $string = str_replace("[$plugin]", $sampleOutput, $string); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/#findComment-818458 Share on other sites More sharing options...
nuttycoder Posted April 24, 2009 Author Share Posted April 24, 2009 anyone any ideas? $plugin contains all the plugins like: blog,sample I need to go from $string = str_replace("[$plugin]", $blogOutput, $string); $string = str_replace("[$plugin]", $sampleOutput, $string); to $string = str_replace("[$plugin]", $someOutputCode, $string); Quote Link to comment https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/#findComment-818522 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.