[英]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.