Page 1 of 1

Problems replacing a variable name for its value in a string

Posted: Fri Feb 15, 2008 7:19 am
by vgarzon
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

Re: Problems replacing a variable name for its value in a string

Posted: Fri Feb 15, 2008 8:00 am
by Zoxive
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.

Re: Problems replacing a variable name for its value in a string

Posted: Fri Feb 15, 2008 12:26 pm
by RobertGonzalez
Moved to PHP - Code.

Re: Problems replacing a variable name for its value in a string

Posted: Fri Feb 15, 2008 1:48 pm
by vgarzon
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

Re: Problems replacing a variable name for its value in a string

Posted: Fri Feb 15, 2008 1:55 pm
by RobertGonzalez
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

Posted: Tue Feb 19, 2008 11:35 am
by vgarzon
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