简体   繁体   English

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

[英]Output with PHP the value of a variable or a pre-defined CONSTANT from a MySQL result string

I have a string stored in MySQL db, like this: 我有一个存储在MySQL数据库中的字符串,如下所示:

The sky is $color and</br> the grass is GRASS_COLOR

When I fetch this from the db and echo I get output 当我从数据库获取并回显时,我得到输出

The sky is $color and 
the grass is GRASS_COLOR

Although I already defined GRASS_COLOR constant (ie. define('GRASS_COLOR', 'green'); ) earlier in the script and $color variable (ie. $color='blue';). 尽管我早在脚本和$ color变量(即$ color ='blue';)中定义了GRASS_COLOR常量(即define('GRASS_COLOR','green');)。 Note, it does properly break the line using html 注意,它确实可以使用html来换行
Is there a way to fetch the string from the database and make it output instead, after fetching it from the db: 从数据库中获取字符串后,有没有一种方法可以从数据库中获取字符串并使其输出:

The sky is blue and 
the grass is green

EDIT: I took Danijel excellent solution and modified it a bit to make it a function, which can now be used on any fetched MySQL (or other) string 编辑:我采取了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);

Maybe if you save the DB strings in sprint_f format, i don't see another way: 也许如果您将数据库字符串以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;

UPDATE UPDATE

Apparently I was a little too hasty with constatation ' i don't see another way '. 显然我对通勤有点太仓促,“ 我看不到另一种方式 ”。 Actually this is definitely achievable with the use of get_defined_vars() and get_defined_constants() functions. 实际上,使用get_defined_vars()get_defined_constants()函数绝对可以实现。 The idea is to collect all user defined variables and constants, and then to replace that in a string. 这个想法是收集所有用户定义的变量和常量,然后将其替换为字符串。 This could even be a simple template engine ( if does not already exists ). 这甚至可以是一个简单的模板引擎(如果尚不存在)。

// 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);

But this isn't a good idea to store such a string in MySQL. 但这不是在MySQL中存储这样的字符串的好主意。 I hope this is only for learning purposes. 我希望这只是出于学习目的。

BTW. BTW。 If you've got many more variables, this is going to be a little complicated. 如果您有更多的变量,这将有些复杂。 But there is something wrong with the whole idea, so let's say that this is a 'stupid workaround'. 但是整个想法有问题,所以我们说这是一个“愚蠢的解决方法”。 If you will write what is the problem and why you are storing something like that in database, we can help to create a better solution. 如果您要写出什么问题以及为什么要在数据库中存储类似的内容,我们可以帮助您创建更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM