[PHP] Re: Parsing variables within string variables

2006-04-07 Thread David Clough
Thanks for all these responses, but unless I'm missing something none of 
them work for what I need. Quotes are irrelevant: with the string "Hello 
$foo" in $bar

   echo "$bar"
   echo $bar

both produce
   
   Hello $foo

and

   echo '$bar'

produces
   
   $bar

I can't use any of the answers like

   'Hello'.$foo

because 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.

If anyone else can provide the silver bullet, I will be very grateful.

   David.

In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (David Clough) wrote:

> I've been bashing my head against the wall on this, and would be glad of 
> help to stop. 
> 
> I have a variable containing a string that contains the names of 
> variables, and want to output the variable with the variables it 
> contains evaluated. E.g. 
> 
>$foo contains 'cat'
>$bar contains 'Hello $foo'
> 
> and I want to output $bar as 
> 
>Hello cat
> 
> The problem is that if I use
> 
>echo $bar
> 
> I just get
> 
>Hello $foo
> 
> Note that $bar is loaded from a database query, so I can't control its 
> contents: I just have to parse it.
> 
> Any help appreciated.
> 
> David.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Parsing variables within string variables

2006-04-07 Thread David Clough
Dear Paul,

this is exactly the problem: the string including the dollar sign comes 
from the database.

The problem I have is that the echo statement parses the $bar reference, 
but not the $foo reference within it.

So

   echo $bar

generates

   Hello $foo

which is better than 

   $bar

but doesn't get as far as

   Hello cat

What I think I need is to send the results of the first echo to a second 
echo for parsing. Is there some way of doing that?

Thanks for any help...

   David.

In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Paul Novitski) wrote:

> I wrote:
> >You need to EVALUATE the string coming from the database:
> >
> >Assuming that $sDataField contains the string 'Hello $foo':
> >
> > $foo = "cat";
> > $sText = eval($sDataField);
> >
> >RESULT: $sText = "Hello cat"
> >
> >http://php.net/eval
> 
> 
> I was assuming that you meant that the string "Hello $foo" -- 
> including the dollar sign -- came from the database.
> 
> If $foo exists as a native PHP variable, I'd want to see your actual 
> code to tell why it's not being properly evaluated by the parser.
> 
> Sometimes you need to use curly braces to help the PHP interpreter 
> differentiate a variable from the surrounding text:
> 
>  $bar = "Hello ${foo}amaran";
> 
> If you escape the dollar sign, the variable won't be evaluated:
> 
>  $bar = "Hello \$foo";
>  RESULT: "Hello $foo"
> 
> Paul

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Parsing variables within string variables

2006-04-08 Thread David Clough

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
[EMAIL PROTECTED]
--
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.