RE: [PHP] Parsing variables within string variables

2006-04-07 Thread Kevin Davies - Bonhurst Consulting
David,

Is this what you're looking for?

$bar = 'Hello '.$bar

HTH,

Kevin


-Original Message-
From: David Clough [mailto:[EMAIL PROTECTED] 
Sent: 07 April 2006 17:37
To: php-general@lists.php.net
Subject: [PHP] Parsing variables within string variables

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

-- 
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 Jay Blanchard
[snip]

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
[/snip]

$bar = 'Hello' . $foo;

--
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 Chrome
Or change the quote style to double ()

Just another option

Dan

(If I'm right this time... I really can't afford 88AUD/hr... :) )
---
http://chrome.me.uk
 

-Original Message-
From: Jay Blanchard [mailto:[EMAIL PROTECTED] 
Sent: 07 April 2006 18:12
To: David Clough; php-general@lists.php.net
Subject: RE: [PHP] Parsing variables within string variables

[snip]

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
[/snip]

$bar = 'Hello' . $foo;

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


__ NOD32 1.1475 (20060406) Information __

This message was checked by NOD32 antivirus system.
http://www.eset.com

-- 
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 Paul Novitski

At 09:37 AM 4/7/2006, David Clough wrote:

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.



David,

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

Paul 


--
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 Paul Novitski

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] Parsing variables within string variables

2006-04-07 Thread Dave Goodchild
Use double quotes/

On 07/04/06, David Clough [EMAIL PROTECTED] 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




--
http://www.web-buddha.co.uk
dynamic web programming from Reigate, Surrey UK

look out for e-karma, our new venture, coming soon!


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