Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to incorporate variables inside variables

Status
Not open for further replies.

hos2

Programmer
Joined
May 6, 2002
Messages
418
Location
NL
I try to make a form where people can use variables like $adress in a form field and that it is translated to values in a database

example someone fills in a form
"adress:$adress"

the $adress variable gets from a database query the next value
$adress="first street"


how can I make it happen that I read
adress: first street

when I print the submitted form variable
????????
 
if you are echoing the variable $address then you know that is what you are doing, so you can just do

Code:
echo 'address: '.$address
 
yep but I have a variabele like

$text="my adress is $adress"

$adress="first street"

then when I say
Print "$text"

the output should be like

"my adress is first street"

ps I have simplified the problem, there are more variables involved here within large text variables. I try to make a htmlform as flexible as possible so that a normal user can also use variables in text like you do with word and the merge function


 
str_replace ( '$address', $address, $text);

Replaces all occurences of the string '$address' with the actual variable
Code:
$address
in the string
Code:
$text
 
try this:
$adress="first street"
$text="my adress is $adress"

then say
Print "$text"

the first time it didnt work because $address is not defined before. u cannot use a vairable's value before defining it...

Known is handfull, Unknown is worldfull
 
Do you want ping-pong game? Client writes var names in form fields, sends form to your (server-side) script, you perform eval() function with client code, send result (var substitution, for example) to client etc...
There are security problems (check user input with PHP code or risk to kill your server with eval of arbitrary user code). If you take KempCGDR's (good) advice you must build scanning and replacing server code for all client form input then add some logic to decide when ping-pong steps ended (all substitutions completed).
Your 2nd post example works fine in PHP script, in one PHP interpreter pass. It is a long way from PHP script to the browser with flexible user forms. Is it OK? May be...
 
Mine doesn't actually need a seperate line for each one, just build them all into arrays, hence:

Code:
str_replace(array('$var1','$var2','$var3'),array($var1,$var2,$var3),$text);
 
Just for reasons of clarity I would suggest to use other placeholder indicators than the dollar sign.
You are talking about parsing variable content into a preexisting text, maybe even HTML. This comes very close to the functionality of a template.
KempCGDR's suggestion to use arrays is good and saves a lot of time - especially if you organize your code.
Suggestion:
1. Use placeholder delimitors like {!myVar!}
2. Have an associative array with the actual values where the key represents the variable name, e.g. $vars['myVar'] and the value the actual value.
3. Loop through all the elements in the array and use a regular expression to replace the placeholder with the actual value.

This is an example for parsing values into existing text/HTML templates.
 
ah the array of str_replace will do the trick I think since I can create arrays dynamicaly from a mysql result. I will try it tonight. and yes I was aware that there are some fundamental problems with variables in variables but I also want to build a system as flexible as possible for the end user. in this case it will also work in a small intranet and not on the internet so disabuse of the system by anyone walking around on the website is not the case. and the other placeholders is also a good idea. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top