Expand Variables in String

Expand Variables in String

am 29.07.2009 22:43:27 von Daniel Kolbo

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,
$str = "some string and some \$var plus other stuff";
echo $str."
";
$var = "Variable Contents";
echo $str."
";
$str_expanded = $str;//would like this to expand $var into $str_expanded
echo $str_expanded."
";
?>

Thanks,
dK
`

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

Re: Expand Variables in String

am 29.07.2009 23:09:38 von Jonathan Tapicer

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 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,
> > $str = "some string and some \$var plus other stuff";
> echo $str."
";
> $var = "Variable Contents";
> echo $str."
";
> $str_expanded = $str;//would like this to expand $var into $str_expanded
> echo $str_expanded."
";
> ?>
>
> Thanks,
> dK
> `
>
> --
> 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: Expand Variables in String

am 29.07.2009 23:47:07 von Daniel Kolbo

Jonathan Tapicer wrote:
> 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 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,
>> >> $str = "some string and some \$var plus other stuff";
>> echo $str."
";
>> $var = "Variable Contents";
>> echo $str."
";
>> $str_expanded = $str;//would like this to expand $var into $str_expanded
>> echo $str_expanded."
";
>> ?>
>>
>> Thanks,
>> dK
>> `
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
nice!

This would allow me to have $str loaded with all kinds of variables and
not have to do a str_replace with each (assuming the quote business is cool)

Why did you have the double \\?
> eval('$str_expanded = "' . str_replace('"', '\\"', $str) . '";');
Isn't the following equivalent?
eval('$str_expanded = "' . str_replace('"', '\"', $str) . '";');

Thanks,
dK
`

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

Re: Expand Variables in String

am 29.07.2009 23:53:35 von Jonathan Tapicer

On Wed, Jul 29, 2009 at 6:47 PM, Daniel Kolbo wrote:
> Jonathan Tapicer wrote:
>> 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 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,
>>> >>> $str = "some string and some \$var plus other stuff";
>>> echo $str."
";
>>> $var = "Variable Contents";
>>> echo $str."
";
>>> $str_expanded = $str;//would like this to expand $var into $str_expanded
>>> echo $str_expanded."
";
>>> ?>
>>>
>>> Thanks,
>>> dK
>>> `
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>
> nice!
>
> This would allow me to have $str loaded with all kinds of variables and
> not have to do a str_replace with each (assuming the quote business is cool)
>
> Why did you have the double \\?
>> eval('$str_expanded = "' . str_replace('"', '\\"', $str) . '";');
> Isn't the following equivalent?
> eval('$str_expanded = "' . str_replace('"', '\"', $str) . '";');
>
> Thanks,
> dK
> `
>

Yes, it's the same, you can use only one \ in that case, sometimes you
have to escape if it is followed by a character that needs to be
escaped, for example you can't do echo '\'; you should do echo '\\';

Jonathan

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