Jump to content

Recommended Posts

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...

 

Link to comment
https://forums.phpfreaks.com/topic/155531-joining-variable-with-string-name/
Share on other sites

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

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);
?>

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.