Re: variable replacement in string [message #178006 is a reply to message #178004] |
Thu, 10 May 2012 13:08 |
M. Strobel
Messages: 386 Registered: December 2011
Karma:
|
Senior Member |
|
|
Am 10.05.2012 15:05, schrieb Goran:
> On 10.5.2012 14:44, 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.
>
> strtr() ?
>
> You could do something like this:
>
> $template = 'Your name is %given_name% %family_name%';
>
> echo strtr($template, array(
> '%given_name%' => 'John',
> '%family_name%' => 'Doe',
> ));
yeah, create my own variable system. No easier way?
/Str.
|
|
|