Problems replacing a variable name for its value in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vgarzon
Forum Newbie
Posts: 3
Joined: Fri Feb 15, 2008 7:06 am

Problems replacing a variable name for its value in a string

Post 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
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

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

Post 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.
User avatar
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

Post by RobertGonzalez »

Moved to PHP - Code.
vgarzon
Forum Newbie
Posts: 3
Joined: Fri Feb 15, 2008 7:06 am

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

Post 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
User avatar
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

Post 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.
vgarzon
Forum Newbie
Posts: 3
Joined: Fri Feb 15, 2008 7:06 am

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

Post 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
Post Reply