我有這個函數可以獲取動態db數據,並且應該將常量和變量從db結果字符串轉換爲它們的匹配值。如果我只是將代碼作爲「直接」代碼運行,而不是作爲函數運行,但是我想使它更通用,所以我試圖將它作爲函數,當它是一個函數時,它只能轉換不變「,而不是變量。這是直接運行正常的代碼。PHP從函數內部訪問未知的外部變量
$text = 'The sky is $color and<br> the grass is GRASS_COLOR'; // Example value taken from the database
$color = 'blue';
define('GRASS_COLOR', 'green');
// collect all defined variables and filter them to get only variables of string and numeric type
$values = array_filter(get_defined_vars(), function($item) {
return is_string($item) || is_numeric($item);
});
// append the dollar sign to keys
$keys = array_map(function($item) {
return '$'.$item;
}, array_keys($values));
// create the final array by comining the arrays $keys and $values
$vars = array_combine($keys, array_values($values));
// relpace names of the variables with values
$text = str_replace(array_keys($vars), array_values($vars), $text);
// collect all constants and replace user defined constants with values
$constants = get_defined_constants(true);
$text = str_replace(array_keys($constants['user']), array_values($constants['user']), $text);
// we are done
echo $text;
我怎樣才能將其轉換爲一個通用的功能,將訪問外未知變量和輸出正確的結果,像在這種情況下:
The sky is blue and
the grass is green
編輯 這是函數:
$foo = 'The sky is $color and</br> the grass is GRASS_COLOR'; // place here value from database
$color = 'blue';
define('GRASS_COLOR', 'green');
function db_fetch_vars_constants($text) {
// collect all defined variables and filter them to get only variables of string and numeric type
$values = array_filter(get_defined_vars(), function($item) {
return is_string($item) || is_numeric($item);
});
// append the dollar sign to keys
$keys = array_map(function($item) {
return '$'.$item;
}, array_keys($values));
// create the final array by comining the arrays $keys and $values
$vars = array_combine($keys, array_values($values));
// relpace names of the variables with values
$text = str_replace(array_keys($vars), array_values($vars), $text);
// collect all constants and replace user defined constants with values
$constants = get_defined_constants(true);
$text = str_replace(array_keys($constants['user']), array_values($constants['user']), $text);
// we are done
echo $text;
}
db_fetch_vars_constants($foo);
編輯#2 作爲@deceze建議,我已經改變了以下功能,它的工作原理。
$color = 'blue';
define('GRASS_COLOR', 'green');
$foo = 'The sky is $color and</br> the grass is GRASS_COLOR'; // place here value from database
function db_fetch_vars_constants($text) {
// collect all defined variables and filter them to get only variables of string and numeric type
//$values = array_filter(get_defined_vars(), function($item) {
$values = array_filter($GLOBALS, function($item) {
return is_string($item) || is_numeric($item);
});
// append the dollar sign to keys
$keys = array_map(function($item) {
return '$'.$item;
}, array_keys($values));
// create the final array by comining the arrays $keys and $values
$vars = array_combine($keys, array_values($values));
// relpace names of the variables with values
$text = str_replace(array_keys($vars), array_values($vars), $text);
// collect all constants and replace user defined constants with values
$constants = get_defined_constants(true);
$text = str_replace(array_keys($constants['user']), array_values($constants['user']), $text);
// we are done
echo $text;
}
db_fetch_vars_constants($foo);
爲什麼這麼複雜?您可以在'$ text'中使用雙引號。變量自動「填充」。你的「功能」在哪裏? – Raptor
不從MySQL數據庫中獲取。看到這裏:http://stackoverflow.com/questions/23596508/output-with-php-the-value-of-a-variable-or-a-pre-defined-constant-from-a-mysql-r由方式,我只是添加了功能 – CreativeMind
不正確。您可以在定義'$ text'之前定義'$ color'。對於您的問題,您可以通過參考/函數參數將變量傳遞到函數中,具體取決於您的使用情況。避免使用Globals。 – Raptor