Dear Paul,
this is exactly the solution I needed, and works as described! Many
thanks for thinking through this with me.
Yours,
David.
On 8 Apr 2006, at 00:05, Paul Novitski wrote:
At 02:41 PM 4/7/2006, David Clough wrote:
I have to parse the string 'Hello $foo' as it comes from the
database: I don't get to construct it.
I did hold out more hope for the eval function, but it seems to me
that
this is for PHP code in a database, not to evaluate variables.
David, please try the eval() route: it will do what you want. You
say, "this is for PHP code in a database, not to evaluate
variables," but evaluating variables is absolutely part of PHP code
processing! Eval() will operate on "$x = 4;" just as easily as on
"Hello $foo."
You should not use eval() frivolously because it presents a
potential vulnerability in your code. You may wish to ensure that
the database text it operates on is first cleansed of any other PHP
syntax -- similarly to the way we should all ensure that any
incoming data is clean before we process it and incorporate it into
our scripts.
Here's an example of variaible evaluation:
_______________________
$bar = "cat";
$foo = "Hello \$bar.";
echo $foo;
RESULT: Hello $bar.
By escaping the $, I have made it a literal character in the text,
the same as if I'd read "Hello $bar" from a database.
_______________________
eval("echo \"$foo\";");
RESULT: Hello cat.
This is equivalent to scripting:
echo "$foo";
I'm using eval() to execute the echo command and interpret the PHP
variable $foo.
_______________________
eval("\$dog = \$bar;");
echo "dog = " . $dog;
RESULT: dog = cat
Here I'm using eval() to set one PHP variable equal to another.
_______________________
You can't simply write:
eval("\$bar;");
or
$x = eval($foo);
because everything inside the eval() parentheses needs to be a
complete PHP statement. The eval() function itself doesn't return
the value of an evaluated expression. To capture the value of an
expression, you must evaluate a complete statement that sets a
variable equal to the expression as above.
_______________________
Clear as mud?
Regards,
Paul
--
Dr. David Clough
Tutor in Ethics and Systematic Theology; Director of Studies
Cranmer Hall, St. John's College, Durham DH1 3RJ, U. K.
Tel. +44 (0)191 334 3858 Fax. +44 (0)191 334 3501
David.Clough@xxxxxxxxxxxx
--
E-MAIL WARNING;
The information in this e-mail is confidential and may be subject to
legal professional privilege. It is intended solely for the
attention and use of the named addressee(s). If you are not the
intended recipient, please notify the sender immediately. Unless you
are the intended recipient or his/her representative you are not
authorised to, and must not, read, copy, distribute, use or retain
this message or any part of it.
At present the integrity of e-mail across the Internet cannot be
guaranteed and messages and documents sent via this medium are
potentially at risk. You should perform your own virus checks before
opening any documents sent with this message. All liability is
excluded to the extent permitted by law for any claims arising from
the use of this medium by St John's College Durham.