使用 PHP 从 MySQL 结果字符串中输出变量或预定义常量的值 [英] Output with PHP the value of a variable or a pre-defined CONSTANT from a MySQL result string

查看:33
本文介绍了使用 PHP 从 MySQL 结果字符串中输出变量或预定义常量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MySQL 数据库中存储了一个字符串,如下所示:

I have a string stored in MySQL db, like this:

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

虽然我已经在脚本和 $color 变量(即 $color='blue';)中定义了 GRASS_COLOR 常量(即,define('GRASS_COLOR', 'green');).请注意,它确实使用 html
正确断行有没有办法从数据库中获取字符串并在从数据库中获取字符串后使其输出:

Although I already defined GRASS_COLOR constant (ie. define('GRASS_COLOR', 'green'); ) earlier in the script and $color variable (ie. $color='blue';). Note, it does properly break the line using 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

我采用了 Danijel 出色的解决方案并对其进行了一些修改以使其成为一个函数,现在可以在任何获取的 MySQL(或其他)字符串上使用该函数

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

$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 格式,我没有看到另一种方式:

Maybe if you save the DB strings in sprint_f format, i don't see another way:

$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() 函数.这个想法是收集所有用户定义的变量和常量,然后将其替换为字符串.这甚至可以是一个简单的模板引擎(如果尚不存在).

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. 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;

这篇关于使用 PHP 从 MySQL 结果字符串中输出变量或预定义常量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆