![](/img/trans.png)
[英]how to get pre-defined value of variable inside function code in PHP wordpress
[英]Output with PHP the value of a variable or a pre-defined CONSTANT from a MySQL result string
我有一個存儲在MySQL數據庫中的字符串,如下所示:
The sky is $color and</br> the grass is GRASS_COLOR
當我從數據庫獲取並回顯時,我得到輸出
The sky is $color and
the grass is GRASS_COLOR
盡管我早在腳本和$ color變量(即$ color ='blue';)中定義了GRASS_COLOR常量(即define('GRASS_COLOR','green');)。 注意,它確實可以使用html來換行
從數據庫中獲取字符串后,有沒有一種方法可以從數據庫中獲取字符串並使其輸出:
The sky is blue and
the grass is green
編輯:我采取了Danijel優秀的解決方案,並對其進行了一些修改,使其成為一個函數,現在可以在任何獲取的MySQL(或其他)字符串上使用它
$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 combining the arrays $keys and $values
$vars = array_combine( $keys, array_values( $values ) );
// replace 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;
}
//call the function. show the result
db_fetch_vars_constants($foo);
也許如果您將數據庫字符串以sprint_f格式保存,我看不到另一種方法:
$color = 'blue';
define('GRASS_COLOR', 'green');
$text = 'The sky is %s and the grass is %s';
$text = sprintf( $text, $color , GRASS_COLOR );
echo $text;
顯然我對通勤有點太倉促,“ 我看不到另一種方式 ”。 實際上,使用get_defined_vars()和get_defined_constants()函數絕對可以實現。 這個想法是收集所有用戶定義的變量和常量,然后將其替換為字符串。 這甚至可以是一個簡單的模板引擎(如果尚不存在)。
// place here value from database
$text = 'The sky is $color and</br> the grass is GRASS_COLOR';
$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;
$string = 'The sky is $color and the grass is GRASS_COLOR';
$string = str_replace('$color', $color, $string);
$string = str_replace('GRASS_COLOR', GRASS_COLOR, $string);
但這不是在MySQL中存儲這樣的字符串的好主意。 我希望這只是出於學習目的。
BTW。 如果您有更多的變量,這將有些復雜。 但是整個想法有問題,所以我們說這是一個“愚蠢的解決方法”。 如果您要寫出什么問題以及為什么要在數據庫中存儲類似的內容,我們可以幫助您創建更好的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.