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
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
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
$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:
$color = 'blue';
define('GRASS_COLOR', 'green');
$text = 'The sky is %s and the grass is %s';
$text = sprintf( $text, $color , GRASS_COLOR );
echo $text;
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;
$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. I hope this is only for learning purposes.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.