Hi,
I've had problemas with an issuea and I don't know if you could help me. I have a lot of telnet commands stored in a table on my DB and several users of my system are creating many more commands all the time.
This Telnet commands have the following notation on the database: nat mapdelete index=$indx (Just to give you a simple example).
On my php script, I give a value for the variable $indx (for example $indx=10) and then I extract this commands using a SQL query, but when I try to execute those commands, instead of executing nat mapdelete index=10, my command stays the way it was on my BD; that is nat mapdelete index=$indx.
Does somebody have had the same problem? Could you help me with this issue?
Cheers
Problems replacing a variable name for its value in a string
Moderator: General Moderators
Re: Problems replacing a variable name for its value in a string
It is because you are getting the code from the database, meaning its a string and not php code.
The best choice is to str_replace() or preg_replace() the variable with the one you want.
The dirty way, is to eval() the string out of the database.
The best choice is to str_replace() or preg_replace() the variable with the one you want.
The dirty way, is to eval() the string out of the database.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Problems replacing a variable name for its value in a string
Moved to PHP - Code.
Re: Problems replacing a variable name for its value in a string
Hey Zoxive!!!!, that was the answer I was looking for!!!!. Thanks a lot. eval() was what I needed.
Unfortunately, I've already made some changes on the script, and the problem is already solved, but it will definitely help me on later developments.
Cheers
Unfortunately, I've already made some changes on the script, and the problem is already solved, but it will definitely help me on later developments.
Cheers
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Problems replacing a variable name for its value in a string
FYI, eval() is very dangerous and can be used against you if your data is ever compromised. I would suggest not using it unless you are building your own PHP code string to be eval'ed. Do not rely on the safety of what is in your database fields.
Re: Problems replacing a variable name for its value in a string
Thanks Everah, I'll have it in mind although I didn't use eval() on my script.
My solution was to extract all the variables that i have on the command (using strpos() and substr()), and search for a variable in php with that name. If I found it, I replaced it. (in case that somebody has the same problem)
thanks a lot anyway
My solution was to extract all the variables that i have on the command (using strpos() and substr()), and search for a variable in php with that name. If I found it, I replaced it. (in case that somebody has the same problem)
thanks a lot anyway