用PHP输出MySQL结果字符串中的变量值或预定义的CONSTANT


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

尽管我已经定义了GRASS_COLOR常量(即define('GRASS_COLOR','green'(;(在脚本的前面和$color变量(即$color='blue';(。注意,它确实使用html正确地换行
有没有一种方法可以从数据库中提取字符串,并在从数据库中获取字符串后将其输出:

The sky is blue and 
the grass is green

EDIT:我采用了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);

如果您将DB字符串保存为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_define_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中存储这样的字符串不是一个好主意。我希望这只是为了学习。

顺便说一句。如果你有更多的变量,这会有点复杂。但整个想法有问题,所以我们可以说这是一个"愚蠢的变通方法"。如果您将写下问题所在以及为什么要在数据库中存储类似的内容,我们可以帮助创建更好的解决方案。