1

Right now I have this code.

<?php
    error_reporting(E_ALL);

    require_once('content_config.php');

    function callback($buffer)
    {
        // replace all the apples with oranges
        foreach ($config as $key => $value)
        {
            $buffer = str_replace($key, $value, $buffer);
        }
        return $buffer;
    }

    ob_start("callback");
?>
some content
<?php

ob_end_flush();

?>

in the content_config.php file:

$config['SiteName'] = 'MySiteName';
$config['SiteAuthor'] = 'thatGuy';

What I want to do is that I want to replace the placeholders that with the key of the config array with its value.

Right now, it doesn't work :(

1 Answer 1

2

your callback function cant see $config. you must either pass it as an argument or declare it global

global $config;

http://php.net/manual/en/language.variables.scope.php

as an aside you can use arrays with str_replace

$buffer = str_replace(array_keys($config), array_values($config), $buffer);

this avoids a loop, which is always good.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I was thinking about the scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.