Re: variable replacement in string [message #178005 is a reply to message #178003] |
Thu, 10 May 2012 13:07 |
M. Strobel
Messages: 386 Registered: December 2011
Karma:
|
Senior Member |
|
|
Am 10.05.2012 15:00, schrieb The Natural Philosopher:
> M. Strobel wrote:
>> Hi,
>>
>> I am still searching a function in PHP to execute variable replacement in strings.
>> Other languages do have this, but for PHP I can only find sprintf() and string
>> replace.
>>
>> I have
>>
>> $t = ' - solved - ';
>> $msg = 'The problem is $t';
>>
>> I want now:
>>
>> echo fxx($msg);
>>
>> print out "The problem is - solved - ".
>>
>> Please don't tell me about $msg = "The problem is $t"; just think of $msg like a
>> template read from a file.
>>
>> /Str.
>
> what is wrong with printf("the problem is %s \n",$t);
relating %s to $t is extra work.
>
>
> or
>
> echo ("the problem is".$t);
>
or
echo 'the problem is ', $t;
be sure I can do it in code. In Tcl, interactive session:
strobel@s114-intel:~> tclsh
% set t " - solved - "
- solved -
% set msg {the problem is $t}
the problem is $t
% puts $msg
the problem is $t
% puts [subst $msg]
the problem is - solved -
%
/Str.
|
|
|