Re: Parsing variables within string variables
| From: | David Clough | Date: | Fri, 07 Apr 2006 21:41:14 +0000 |
| Subject: | Re: Parsing variables within string variables | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-233484@lists.php.net to get a copy of this message | ||
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 <David.Clough-6046F3.17371407042006@lists.php.net>,
David.Clough@durham.ac.uk (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.