I've been searching for this for a while now.. It's probably very simple but I just can't seem to get it right.
My website uses a file with translations. Every translation has it's own variable.
Like: $_text_login
(which translates to: Login or Aanmelden or Derp).
I've made a menu table within my database. One of the menu items has the text: $_text_login
Obviously, when I get the menu information, it doesn't show the currently set value of the variable, it just shows $_text_login
.
I've tried eval() but that doesn't work..
Can anyone help?
I would not recommend this approach and I would santize the variable name, but from the sounds of it, you could achieve what you are saying with:
echo eval('return '. $item . ';');
Where $item would be the result from the database.
Lets say $row
is the result you received from the database, and text
is the name of the column containing the variable. Then you would do this:
echo $$row['text'];
If $row['text']
is _text_login
this should output the value of $_text_login
. You should not have the $ in the database.
According to documentation of PHP you can just use $$varname
structure: I mean:
<?php
$fruit = "apple";
$variable = "fruit";
print_r($$variable); //outputs apple
Got it! Thanks..
Eval did work eventually.. Because I matched the actual variables... I feel stupid now..
while($main_obj = $query_mainmenuitems->fetch_object()) {
if (preg_match('/\$text_/',$main_obj->value)) {
$mainmenu_title = eval('return '.$main_obj->value.';');
} else {
$mainmenu_title = $main_obj->value;
}
}
It shouldn't be $_text_logon .. all my translations have $text_[whatever]