Re: Expand Variables in String
| From: | Jonathan Tapicer | Date: | Wed, 29 Jul 2009 21:09:38 +0000 |
| Subject: | Re: Expand Variables in String | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296033@lists.php.net to get a copy of this message | ||
Use eval, like this:
eval('$str_expanded = "' . str_replace('"', '\\"',
$str) . '";');
The str_replace is used because you could have a " inside $str and it
would break the string, if you are sure the $str has no " inside you
can omit it.
Jonathan
On Wed, Jul 29, 2009 at 5:43 PM, Daniel Kolbo<kolb0057@umn.edu> wrote:
> Hello,
>
> Is it possible to force a string to expand variable names after the
> string has been set and before the variable's been defined without
> having to do a string replace or preg_replace?
>
> for example,
> <?php
> $str = "some string and some \$var plus other stuff";
> echo $str."<br />";
> $var = "Variable Contents";
> echo $str."<br />";
> $str_expanded = $str;//would like this to expand $var into $str_expanded
> echo $str_expanded."<br />";
> ?>
>
> Thanks,
> dK
> `
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>