2010-05-19 9 views
1

지금이 코드가 있습니다. content_config.php 파일 PHP에서 출력 변수를 바꾸기

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

?> 

는 :

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

내가 뭘 원하는 내가 자리를 대체 할 것입니다 자사의 값으로 설정 배열의 키. 지금

, 그것은.

답변

2

콜백 함수는 $ 설정을 볼 수 없습니다 :(작동하지 않습니다 당신은 인수로 전달하거나 선언해야 하나 그것이 글로벌

global $config; 

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

배열을 str_replace와 함께 사용할 수 있습니다.

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

이것은 항상 좋은 루프를 피합니다.

+0

그래, 범위에 대해 생각해 보았습니다. –

관련 문제